firstNotNullOfOrNull

Platform and version requirements: JVM (1.5), JS (1.5), Native (1.5)
inline fun <T, R : Any> Array<out T>.firstNotNullOfOrNull(
    transform: (T) -> R?
): R?

Returns the first non-null value produced by transform function being applied to elements of this array in iteration order, or null if no non-null value was produced.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
data class Rectangle(val height: Int, val width: Int) {
    val area: Int get() = height * width
}

val rectangles = listOf(
    Rectangle(3, 4),
    Rectangle(1, 8),
    Rectangle(6, 3),
    Rectangle(4, 3),
    Rectangle(5, 7)
)

val largeArea = rectangles.firstNotNullOf { it.area.takeIf { area -> area >= 15 } }
val largeAreaOrNull = rectangles.firstNotNullOfOrNull { it.area.takeIf { area -> area >= 15 } }

println(largeArea) // 18
println(largeAreaOrNull) // 18

// val evenLargerArea = rectangles.firstNotNullOf { it.area.takeIf { area -> area >= 50 } } // will fail with NoSuchElementException
val evenLargerAreaOrNull = rectangles.firstNotNullOfOrNull { it.area.takeIf { area -> area >= 50 } }

println(evenLargerAreaOrNull) // null
//sampleEnd
}
Platform and version requirements: JVM (1.5), JS (1.5), Native (1.5)
inline fun <T, R : Any> Iterable<T>.firstNotNullOfOrNull(
    transform: (T) -> R?
): R?

Returns the first non-null value produced by transform function being applied to elements of this collection in iteration order, or null if no non-null value was produced.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
data class Rectangle(val height: Int, val width: Int) {
    val area: Int get() = height * width
}

val rectangles = listOf(
    Rectangle(3, 4),
    Rectangle(1, 8),
    Rectangle(6, 3),
    Rectangle(4, 3),
    Rectangle(5, 7)
)

val largeArea = rectangles.firstNotNullOf { it.area.takeIf { area -> area >= 15 } }
val largeAreaOrNull = rectangles.firstNotNullOfOrNull { it.area.takeIf { area -> area >= 15 } }

println(largeArea) // 18
println(largeAreaOrNull) // 18

// val evenLargerArea = rectangles.firstNotNullOf { it.area.takeIf { area -> area >= 50 } } // will fail with NoSuchElementException
val evenLargerAreaOrNull = rectangles.firstNotNullOfOrNull { it.area.takeIf { area -> area >= 50 } }

println(evenLargerAreaOrNull) // null
//sampleEnd
}
Platform and version requirements: JVM (1.5), JS (1.5), Native (1.5)
inline fun <K, V, R : Any> Map<out K, V>.firstNotNullOfOrNull(
    transform: (Entry<K, V>) -> R?
): R?

Returns the first non-null value produced by transform function being applied to entries of this map in iteration order, or null if no non-null value was produced.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
data class Rectangle(val height: Int, val width: Int) {
    val area: Int get() = height * width
}

val rectangles = listOf(
    Rectangle(3, 4),
    Rectangle(1, 8),
    Rectangle(6, 3),
    Rectangle(4, 3),
    Rectangle(5, 7)
)

val largeArea = rectangles.firstNotNullOf { it.area.takeIf { area -> area >= 15 } }
val largeAreaOrNull = rectangles.firstNotNullOfOrNull { it.area.takeIf { area -> area >= 15 } }

println(largeArea) // 18
println(largeAreaOrNull) // 18

// val evenLargerArea = rectangles.firstNotNullOf { it.area.takeIf { area -> area >= 50 } } // will fail with NoSuchElementException
val evenLargerAreaOrNull = rectangles.firstNotNullOfOrNull { it.area.takeIf { area -> area >= 50 } }

println(evenLargerAreaOrNull) // null
//sampleEnd
}

© 2010–2021 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/first-not-null-of-or-null.html