Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lastornull
arraylist
kotlin
communitycreator

How to use the lastOrNull() method of the ArrayList in Kotlin

Programming Bytes

Overview

The lastOrNull() method can be used in two ways:

  1. To get the last element of the ArrayList. If the ArrayList is empty, then null is returned.
  2. To get the last element of the ArrayList that satisfies the provided predicate functionA Predicate is a functional interface, which takes one argument and returns either true or false based on the defined condition.. If no element matches the provided predicate function, null is returned.
g array 1 2 3 4
List with the last element highlighted

Syntax without the predicate function

// to get the last element 
fun <T> List<T>.lastOrNull(): T?

Parameters

This method doesn’t take any arguments.

Return value

This method returns the last element of the ArrayList, if it is not empty. If the ArrayList is empty, then a null value is returned.

Syntax with the predicate function

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

Parameters

This method takes a predicate function as an argument.

Return value

This method returns the last element from the ArrayList that satisfies the provided predicate.

If no element matches the given predicate function, then null is returned.

Code example

The code written below demonstrates how we use the lastOrNull() method:

fun main() {
  // declaring an empty list
  var list = ArrayList<Int>()
  println("The ArrayList is $list")

  // getting the last element of the list
  var lastEle = list.lastOrNull();
  println("The last element is $lastEle");

  // adding elements to the list
  list.add(1)
  list.add(2)
  list.add(3)
  list.add(4)
  println("\nThe ArrayList is $list")

  //getting the last element of the list
  lastEle = list.lastOrNull();
  println("The last element is $lastEle");
  
  // getting the last even element from the list
  lastEle = list.lastOrNull({it % 2 == 0 })
  println("\nThe last even element is $lastEle")

  // getting the last element that is greater than 4
  lastEle = list.lastOrNull({it > 4})
  println("\nThe lastEle element > 4 is $lastEle")
}
Using the lastOrNull() method

Code explanation

  • Line 3: We create a new ArrayList object with the name list. This list contains no elements at this point.

  • Line 5: We use the lastOrNull method to get the last element of the list. Since the list is empty, null is returned.

  • Lines 8–11: We add four elements to the list, using the add method. Now, the list is [1,2,3,4].

  • Line 13: We use the lastOrNull method to get the last element of the list. Since the list is not empty anymore, its last element (4) is returned.

  • Line 16: The lastOrNull method is given a predicate function as an argument. This function will return true if there is an even element present in the ArrayList. The lastOrNull method will return the last even element of the ArrayList. In our case, 4 will be returned.

  • Line 19: The lastOrNull method is given a predicate function as an argument. This function will return true if the list has an element that is greater than 4. In our ArrayList, there is no element that is greater than 4, so null is returned.

RELATED TAGS

lastornull
arraylist
kotlin
communitycreator
RELATED COURSES

View all Courses

Keep Exploring