last

fun <T> Array<out T>.last(): T
fun ByteArray.last(): Byte
fun ShortArray.last(): Short
fun IntArray.last(): Int
fun LongArray.last(): Long
fun FloatArray.last(): Float
fun DoubleArray.last(): Double
fun BooleanArray.last(): Boolean
fun CharArray.last(): Char

Returns the last element.

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
}

Throws

if the array is empty.


inline fun <T> Array<out T>.last(predicate: (T) -> Boolean): T
inline fun ByteArray.last(predicate: (Byte) -> Boolean): Byte
inline fun ShortArray.last(predicate: (Short) -> Boolean): Short
inline fun IntArray.last(predicate: (Int) -> Boolean): Int
inline fun LongArray.last(predicate: (Long) -> Boolean): Long
inline fun FloatArray.last(predicate: (Float) -> Boolean): Float
inline fun DoubleArray.last(predicate: (Double) -> Boolean): Double
inline fun BooleanArray.last(predicate: (Boolean) -> Boolean): Boolean
inline fun CharArray.last(predicate: (Char) -> Boolean): Char
inline fun <T> Iterable<T>.last(predicate: (T) -> Boolean): T
inline fun <T> List<T>.last(predicate: (T) -> Boolean): T

Returns the last element matching the given predicate.

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
}

Throws

if no such element is found.


fun <T> Iterable<T>.last(): T

Returns the last element.

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
}

Throws

if the collection is empty.


fun <T> List<T>.last(): T

Returns the last element.

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
}

Throws

if the list is empty.


inline fun UIntArray.last(): UInt
inline fun ULongArray.last(): ULong
inline fun UByteArray.last(): UByte
inline fun UShortArray.last(): UShort

Returns the last element.

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
}

Throws

if the array is empty.


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

Returns the last element matching the given predicate.

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
}

Throws

if no such element is found.