.lastOption
to access the last element SC-P1001Certain Scala structures such as List
implement methods such as .reverse
that allows you to reverse the contents of a structure. This however can be an expensive operation and depends upon the kind of structure and the number of elements in it. Therefore, directly accessing the last element via .lastOption
is more concise and performant than reversing an entire collection and then accessing the first element.
val lastElement = elements.reverse.headOption
val lastElement = elements.lastOption