Kotlin Array.lastOrNull()

Kotlin Array.lastOrNull() Function

In Kotlin, the Array.lastOrNull() function is used to obtain the last element of the given array that satisfies a given predicate. It returns the last element that matches the predicate or null if no such element is found.

If no predicate is given, then lastOrNull() function returns the last element in the array or null if the array is empty.

This function is useful when you want to get the last element that meets a certain condition without throwing an exception if no such element exists.

In this tutorial, we’ll explore the syntax of the lastOrNull() function and provide examples of its usage in Kotlin arrays, both with and without a predicate.

Syntax

The syntax of the lastOrNull() function is as follows:

fun <T> Array<out T>.lastOrNull(): T?
fun <T> Array<out T>.lastOrNull(predicate: (T) -> Boolean): T?

where

ParameterDescription
predicateA lambda function that takes the element of the array as an argument and returns true or false.
Parameters of Array lastOrNull() function

The return type is T?, indicating that the function can return either the last element that satisfies the predicate or null.

Examples for Array.lastOrNull() Function

1. Using lastOrNull() with a Predicate

In this example, we’ll use lastOrNull() with a predicate to obtain the last even number from an array of integers or null if no even number is found.

Kotlin Program

fun main() {
    val numbersArray = arrayOf(1, 3, 5, 6, 8, 9)

    // Using lastOrNull() with a predicate to obtain the last even number or null
    val lastEvenNumber = numbersArray.lastOrNull { it % 2 == 0 }

    // Printing the original array and the result
    println("Numbers Array: \n${numbersArray.joinToString(", ")}\n")
    println("Last Even Number or Null\n$lastEvenNumber")
}

Output

Numbers Array: 
1, 3, 5, 6, 8, 9

Last Even Number or Null
8

2. Using lastOrNull() where no element satisfies given predicate

In this example, we’ll use lastOrNull() with a predicate to obtain the last even number from an array of integers or null if no even number is found. We have take the array of numbers such that there is not even a single element that satisfies the given predicate. As a result, null should be returned by the lastOrNull() function.

Kotlin Program

fun main() {
    val numbersArray = arrayOf(1, 3, 5, 9)

    // Using lastOrNull() with a predicate to obtain the last even number or null
    val lastEvenNumber = numbersArray.lastOrNull { it % 2 == 0 }

    // Printing the original array and the result
    println("Numbers Array: \n${numbersArray.joinToString(", ")}\n")
    println("Last Even Number or Null\n$lastEvenNumber")
}

Output

Numbers Array: 
1, 3, 5, 9

Last Even Number or Null
null

3. Using lastOrNull() without a Predicate

In this example, we’ll use lastOrNull() without a predicate to obtain the last element of an array of numbers or null if the array is empty.

Kotlin Program

fun main() {
    val numbersArray = arrayOf(1, 3, 5, 6, 8, 9)

    // Using lastOrNull() without a predicate
    val lastNumber = numbersArray.lastOrNull()

    // Printing the original array and the result
    println("Numbers Array: \n${numbersArray.joinToString(", ")}\n")
    println("Last Number or Null\n$lastNumber")
}

Output

Numbers Array: 
1, 3, 5, 6, 8, 9

Last Number or Null
9

Summary

In this tutorial, we’ve covered the lastOrNull() function in Kotlin arrays, its syntax, and how to use it to obtain the last element based on a predicate or get null if no such element is found. This function is a safe alternative to last() when dealing with arrays to prevent exceptions in cases where the last element might not exist.