Returns the last element.
The operation is terminal.
Since Kotlin
1.0Throws
if the sequence is empty.
Samples
import kotlin.test.*
fun main() {
val list = listOf(1, 2, 3, 4)
println(list.last())
println(list.last { it % 2 == 1 })
println(list.lastOrNull { it < 0 })
val emptyList = emptyList<Int>()
println(emptyList.lastOrNull())
}
Target: JVMRunning on v.2.1.20
Returns the last element matching the given predicate.
The operation is terminal.
Since Kotlin
1.0Throws
if no such element is found.
Samples
import kotlin.test.*
fun main() {
val list = listOf(1, 2, 3, 4)
println(list.last())
println(list.last { it % 2 == 1 })
println(list.lastOrNull { it < 0 })
val emptyList = emptyList<Int>()
println(emptyList.lastOrNull())
}
Target: JVMRunning on v.2.1.20