firstOrNull

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

Reads only the first element. 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 first element in iteration order, or null when empty.

See also


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

Returns the first matching element, or null when no element matches predicate.

Stops at the first 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 first accepted element, or null when empty or unmatched.

Parameters

predicate

test returning true to select an element.

See also


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

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

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

Receiver

list to read.

Return

the element at index zero, or null for an empty list.

See also