lastOrNull

Common
JVM
JS
Native
1.0
fun <T> Array<out T>.lastOrNull(): T?
fun ByteArray.lastOrNull(): Byte?
fun ShortArray.lastOrNull(): Short?
fun IntArray.lastOrNull(): Int?
fun LongArray.lastOrNull(): Long?
fun FloatArray.lastOrNull(): Float?
fun DoubleArray.lastOrNull(): Double?
fun BooleanArray.lastOrNull(): Boolean?
fun CharArray.lastOrNull(): Char?
@ExperimentalUnsignedTypes fun UIntArray.lastOrNull(): UInt?
@ExperimentalUnsignedTypes fun ULongArray.lastOrNull(): ULong?
@ExperimentalUnsignedTypes fun UByteArray.lastOrNull(): UByte?
@ExperimentalUnsignedTypes fun UShortArray.lastOrNull(): UShort?

Returns the last element, or null if the array is empty.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf(1, 2, 3, 4)
println(list.last()) // 4
println(list.last { it % 2 == 1 }) // 3
println(list.lastOrNull { it < 0 }) // null
// list.last { it < 0 } //  will fail

val emptyList = emptyList<Int>()
println(emptyList.lastOrNull()) // null
// emptyList.last() //  will fail
//sampleEnd
}
Common
JVM
JS
Native
1.0
inline fun <T> Array<out T>.lastOrNull(
    predicate: (T) -> Boolean
): T?
inline fun ByteArray.lastOrNull(
    predicate: (Byte) -> Boolean
): Byte?
inline fun ShortArray.lastOrNull(
    predicate: (Short) -> Boolean
): Short?
inline fun IntArray.lastOrNull(
    predicate: (Int) -> Boolean
): Int?
inline fun LongArray.lastOrNull(
    predicate: (Long) -> Boolean
): Long?
inline fun FloatArray.lastOrNull(
    predicate: (Float) -> Boolean
): Float?
inline fun DoubleArray.lastOrNull(
    predicate: (Double) -> Boolean
): Double?
inline fun BooleanArray.lastOrNull(
    predicate: (Boolean) -> Boolean
): Boolean?
inline fun CharArray.lastOrNull(
    predicate: (Char) -> Boolean
): Char?
inline fun <T> Iterable<T>.lastOrNull(
    predicate: (T) -> Boolean
): T?
inline fun <T> List<T>.lastOrNull(
    predicate: (T) -> Boolean
): T?
@ExperimentalUnsignedTypes inline fun UIntArray.lastOrNull(
    predicate: (UInt) -> Boolean
): UInt?
@ExperimentalUnsignedTypes inline fun ULongArray.lastOrNull(
    predicate: (ULong) -> Boolean
): ULong?
@ExperimentalUnsignedTypes inline fun UByteArray.lastOrNull(
    predicate: (UByte) -> Boolean
): UByte?
@ExperimentalUnsignedTypes inline fun UShortArray.lastOrNull(
    predicate: (UShort) -> Boolean
): UShort?

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

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf(1, 2, 3, 4)
println(list.last()) // 4
println(list.last { it % 2 == 1 }) // 3
println(list.lastOrNull { it < 0 }) // null
// list.last { it < 0 } //  will fail

val emptyList = emptyList<Int>()
println(emptyList.lastOrNull()) // null
// emptyList.last() //  will fail
//sampleEnd
}
Common
JVM
JS
Native
1.0
fun <T> Iterable<T>.lastOrNull(): T?

Returns the last element, or null if the collection is empty.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf(1, 2, 3, 4)
println(list.last()) // 4
println(list.last { it % 2 == 1 }) // 3
println(list.lastOrNull { it < 0 }) // null
// list.last { it < 0 } //  will fail

val emptyList = emptyList<Int>()
println(emptyList.lastOrNull()) // null
// emptyList.last() //  will fail
//sampleEnd
}
Common
JVM
JS
Native
1.0
fun <T> List<T>.lastOrNull(): T?

Returns the last element, or null if the list is empty.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf(1, 2, 3, 4)
println(list.last()) // 4
println(list.last { it % 2 == 1 }) // 3
println(list.lastOrNull { it < 0 }) // null
// list.last { it < 0 } //  will fail

val emptyList = emptyList<Int>()
println(emptyList.lastOrNull()) // null
// emptyList.last() //  will fail
//sampleEnd
}