lastOrNull

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?

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

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3, 4)
assertPrints(list.last(), "4")
assertPrints(list.last { it % 2 == 1 }, "3")
assertPrints(list.lastOrNull { it < 0 }, "null")
assertFails { list.last { it < 0 } }

val emptyList = emptyList<Int>()
assertPrints(emptyList.lastOrNull(), "null")
assertFails { emptyList.last() } 
   //sampleEnd
}

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?

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

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3, 4)
assertPrints(list.last(), "4")
assertPrints(list.last { it % 2 == 1 }, "3")
assertPrints(list.lastOrNull { it < 0 }, "null")
assertFails { list.last { it < 0 } }

val emptyList = emptyList<Int>()
assertPrints(emptyList.lastOrNull(), "null")
assertFails { emptyList.last() } 
   //sampleEnd
}

fun <T> Iterable<T>.lastOrNull(): T?

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

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3, 4)
assertPrints(list.last(), "4")
assertPrints(list.last { it % 2 == 1 }, "3")
assertPrints(list.lastOrNull { it < 0 }, "null")
assertFails { list.last { it < 0 } }

val emptyList = emptyList<Int>()
assertPrints(emptyList.lastOrNull(), "null")
assertFails { emptyList.last() } 
   //sampleEnd
}

fun <T> List<T>.lastOrNull(): T?

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

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3, 4)
assertPrints(list.last(), "4")
assertPrints(list.last { it % 2 == 1 }, "3")
assertPrints(list.lastOrNull { it < 0 }, "null")
assertFails { list.last { it < 0 } }

val emptyList = emptyList<Int>()
assertPrints(emptyList.lastOrNull(), "null")
assertFails { emptyList.last() } 
   //sampleEnd
}

fun UIntArray.lastOrNull(): UInt?
fun ULongArray.lastOrNull(): ULong?
fun UByteArray.lastOrNull(): UByte?
fun UShortArray.lastOrNull(): UShort?

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

Since Kotlin

1.3

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3, 4)
assertPrints(list.last(), "4")
assertPrints(list.last { it % 2 == 1 }, "3")
assertPrints(list.lastOrNull { it < 0 }, "null")
assertFails { list.last { it < 0 } }

val emptyList = emptyList<Int>()
assertPrints(emptyList.lastOrNull(), "null")
assertFails { emptyList.last() } 
   //sampleEnd
}

inline fun UIntArray.lastOrNull(predicate: (UInt) -> Boolean): UInt?
inline fun ULongArray.lastOrNull(predicate: (ULong) -> Boolean): ULong?
inline fun UByteArray.lastOrNull(predicate: (UByte) -> Boolean): UByte?
inline fun UShortArray.lastOrNull(predicate: (UShort) -> Boolean): UShort?

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

Since Kotlin

1.3

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3, 4)
assertPrints(list.last(), "4")
assertPrints(list.last { it % 2 == 1 }, "3")
assertPrints(list.lastOrNull { it < 0 }, "null")
assertFails { list.last { it < 0 } }

val emptyList = emptyList<Int>()
assertPrints(emptyList.lastOrNull(), "null")
assertFails { emptyList.last() } 
   //sampleEnd
}