firstNotNullOf所在位置是kotlin.collections.firstNotNullOf,其相關用法介紹如下。

用法一

inline fun <T, R : Any> Array<out T>.firstNotNullOf(
    transform: (T) -> R?
): R

返回由transform函數產生的第一個非空值,該函數按迭代順序應用於此數組的元素,如果沒有產生非空值,則拋出NoSuchElementException

例子:

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
}

輸出:

18
18
null

用法二

inline fun <T, R : Any> Iterable<T>.firstNotNullOf(
    transform: (T) -> R?
): R

返回由transform函數產生的第一個非空值,該函數按迭代順序應用於此集合的元素,如果沒有產生非空值,則拋出NoSuchElementException

例子:

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
}

輸出:

18
18
null

用法三

inline fun <K, V, R : Any> Map<out K, V>.firstNotNullOf(
    transform: (Entry<K, V>) -> R?
): R

返回由transform函數產生的第一個非空值,該函數按迭代順序應用於此映射的條目,如果沒有產生非空值,則拋出NoSuchElementException

例子:

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
}

輸出:

18
18
null