getFirst

inline fun <T> Iterable<T>.getFirst(defaultValue: T): T(source)

Returns the first element in this or defaultValue if the iterable is empty. The Iterators analog to this method is Iterators.getNext.

If no default value is desired (and the caller instead wants a NoSuchElementException to be thrown), it is recommended that iterable.iterator().next() is used instead.

To get the only element in a single-element this, consider using .getOnlyElement or .getOnlyElement instead.

Stream equivalent: stream.findFirst().orElse(defaultValue)

Return

the first element of this or the default value

See also

Parameters

defaultValue

the default value to return if the iterable is empty