lastOrNull

Returns the last element, or null when this iterable is empty.

Traverses the entire iterable. For nullable elements, a null result may also be a selected null element. Exceptions from iteration propagate to the caller.

Receiver

elements to visit in their iteration order.

Return

the last element in iteration order, or null when empty.


inline fun <T> Iterable<T>.lastOrNull(predicate: (T) -> Boolean): T?(source)

Returns the last matching element after traversing this iterable in iteration order.

Tests every element and keeps the most recent match. For nullable elements, a null result may also be a selected null element. Exceptions from iteration or the callback propagate to the caller. The inline callback supports non-local returns.

Receiver

elements to visit in their iteration order.

Return

the last accepted element, or null when empty or unmatched.

Parameters

predicate

test returning true to select an element.


fun <T> List<T>.lastOrNull(): T?(source)

Returns the last element, or null when this list is empty.

For nullable element types, null can also be the last stored element.

Receiver

list to read.

Return

the element at size minus one, or null for an empty list.

See also


inline fun <T> List<T>.lastOrNull(predicate: (T) -> Boolean): T?(source)

Returns the last element satisfying predicate, or null when no element matches.

Visits elements from the last index towards zero and stops at the first match. Empty lists never invoke predicate. A matching null element is indistinguishable from no match. The callback is inline and supports non-local returns; its exceptions propagate to the caller.

Receiver

list to search in reverse order.

Return

the last matching element, or null if the list is empty or has no match.

Parameters

predicate

test applied to each visited element.