find

inline fun <T> Array<out T>.find(predicate: (T) -> Boolean): T?(source)
inline fun ByteArray.find(predicate: (Byte) -> Boolean): Byte?(source)
inline fun ShortArray.find(predicate: (Short) -> Boolean): Short?(source)
inline fun IntArray.find(predicate: (Int) -> Boolean): Int?(source)
inline fun LongArray.find(predicate: (Long) -> Boolean): Long?(source)
inline fun FloatArray.find(predicate: (Float) -> Boolean): Float?(source)
inline fun DoubleArray.find(predicate: (Double) -> Boolean): Double?(source)
inline fun BooleanArray.find(predicate: (Boolean) -> Boolean): Boolean?(source)
inline fun CharArray.find(predicate: (Char) -> Boolean): Char?(source)
inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean): T?(source)

Returns the first element matching the given predicate, or null if no such element was found.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val numbers = listOf(1, 2, 3, 4, 5, 6, 7)
val firstOdd = numbers.find { it % 2 != 0 }
val lastEven = numbers.findLast { it % 2 == 0 }

println(firstOdd) // 1
println(lastEven) // 6 
   //sampleEnd
}