Java Stream findFirst()
Overview
How would you find the first even number greater than a given number in a list of even numbers? Make sure you don't tell the audience that you'll loop through the list and return the first element that matches the given number. It's true, but you shouldn't do that in Java 8. While that's fine for Java 7 or earlier versions, Java 8 offers you many more options, one of which is Stream. Using the Stream class with the findFirst() method you can easily get the job done.
In Java, the findFirst() method is a stream terminal operation that returns an Optional containing the first element of a stream that matches a specified condition. If no elements match the condition, an empty Optional is returned.
The findFirst() method can be called on any stream of objects or primitives.
Syntax of Java Stream findFirst()
The syntax of the findFirst() method is shown below
Syntax:
The optional here represents a container object which may be null and T represents the object type.
Return value of Java Stream findFirst()
The findFirst() method in java returns an optional.
The Optional here describes the first element of the given stream if the stream is non-empty(has elements inside it), else it returns an empty optional.
Exceptions of Java Stream findFirst()
The findFirst() method in java throws the following exceptions
NullPointerException
Stream.findFirst() throws java.lang.NullPointerException if it finds the first element to be null.
To make our program throw a null pointer exception we have explicitly kept the first element as null.
Example:
Output:
Explanation:
In the above code, we are on purpose declaring a null elemnt at first so when our findFirst() method goes and searches for the first element it returns an NullPointerException.
NoSuchElementException
A java.util.NoSuchElementException is thrown if Stream.findFirst() fails to find an element, returning an empty Optional. Before retrieving an element, it is recommended to always check if it is present by using Optional.isPresent().
Example:
Output:
Explanation:
In the above code, we are on purpose declaring a empty list so when our findFirst() method goes and searches for the first element it returns a NoSuchElementException .
What is Java Stream findFirst()?
This method returns the first element from the Stream when there is an encounter order. Streams may or may not have a defined encounter order, according to java. util.streams package documentation. It depends on both the source and the intermediate operations. In the absence of an encounter order, any element from the stream will be returned
Example
Let's solve the problem we proposed in the Overview section of this article. Find the first even element in a list.
Output:
Code Explanation:
- In the above example, we have a list of numbers and we want to find the first even number in the list.
- We use the stream() method to convert the list into a stream, and then we use the filter() method to filter all the even numbers.
- Finally, we call the findFirst() method to get the first even number.
More Examples
Example 1
Finding the first element using the findFirst() method with a sequential stream
An example of the findFirst() method using a sequential stream is provided below.
Output:
Code Explanation:
In the above code, we have initialized a list of string as a sequential stream and have found the first one using the findFirst() method.
Example 2
Let's see an example of using findFirst() method with a specified condition.
Output:
Explanation:
- In the above example, we have a list of fruits and we want to find the first fruit that starts with the letter "c".
- We use the stream() method to convert the list into a stream, and then we use the filter() method to filter out all the fruits that start with "c".
- Finally, we call the findFirst() method to get the first fruit that matches the condition.
Stream findFirst() vs findAny()
In the findFirst() method the first element of a stream is returned. In the absence of an encounter order, any element is returned, as it is ambiguous as to which element is the first.
With findAny(), any element of the stream is returned - the same as findFirst(), but without the consideration of encounter order.
There might be a situation in which you need only the first element from a stream in that case findFirst() function comes in handy.
Conclusion
- The findFirst() method is a terminal short-circuiting method.
- The findFirst() method returns an optional.
- The optional contains the first element of the stream after applying the filter condition if it is non-empty.
- If the stream has a defined order then the findFirst() function returns the first element else it returns any element.
- Stream.findFirst() throws java.lang.NullPointerException if it finds the first element to be null.
- A java.util.NoSuchElementException is thrown if Stream.findFirst() fails to find an element, returning an empty Optional.