
The method below demonstrates how to use the firstOrNull() method. If no element matches the given predicate function, then null is returned. This method returns the first element from the list that satisfies the provided the predicate. This method takes a predicate function as an argument. Syntax to get the first element that matches the predicate fun List.firstOrNull( This is the best approach when we are using the 'var' variable. The following example demonstrates how this operator works. It can only be used when we are sure that we are refereeing to a non-NULL able value. Returns the first element if the ArrayList is not empty. You can use the '.let' operator in Kotlin to check if the value of a variable is NULL. Syntax to get the first element // to get first element If no element matches the provided predicate function, then null is returned. To get the first element of the ArrayList that satisfies the provided predicate function Predicate is a Functional interface, which takes one argument and returns either true or false based on the condition defined.

If the ArrayList is empty, then null is returned.

