last Or Null
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 DoubleArray.lastOrNull(predicate: (Double) -> Boolean): Double?
Content copied to clipboard
inline fun BooleanArray.lastOrNull(predicate: (Boolean) -> Boolean): Boolean?
Content copied to clipboard
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
}
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
}
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
}
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 UShortArray.lastOrNull(predicate: (UShort) -> Boolean): UShort?
Content copied to clipboard
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
}