
The task of the lambda function is to return a new accumulator. An individual item from our collection.An accumulator, which has the same type as our collection, and.For example, this could be a list of cities in Germany, and we have another list of German license plates that correspond to those cities:Īs seen in the example above, we call the reduce function with a lambda block which receives two parameters: Let's learn about a way to combine two collections, and create a new one from them – it's time to zip!Īssume we have two collections, where the elements at each index are somehow related. So far, we have always looked at a single collection, and what we can do with it. If you are doing an operation on a list, which in turn generates a collection for each one of the input elements, consider if flatMap can help you simplify your code! As desired, we end up with a plain list of elements – the list of characters from the names of the original collection. Next, that collection of collections gets flattened. In the example above, the function that we provide creates a list for each element in our input collection, containing the letters of the original string. The method below demonstrates how to use the firstOrNull() method.Enter fullscreen mode Exit fullscreen mode 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( Returns the first element if the ArrayList is not empty. Syntax to get the first element // to get first element If no element matches the provided predicate function, then null is returned.

If the ArrayList is empty, then null is returned. To get the first element of the ArrayList.

The firstOrNull() method can be used in two ways.
