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

用法:

inline fun <T> Sequence<T>.findLast(
    predicate: (T) -> Boolean
): T?

返回匹配給定 predicate 的最後一個元素,如果沒有找到這樣的元素,則返回 null

操作是 terminal

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//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
}

輸出:

1
6