This write-up will demonstrate the working of the Java “findFirst()” method.
What is the Java Stream “findFirst()” Method?
The Stream “findFirst()” method gives an “Optional” corresponding to the first stream element, or an “Optional”, which is empty in the case of the stream being null.
Syntax
In the above syntax:
- “Optional” refers to the object(container) that can or cannot get a non-null value.
- “T” corresponds to the object’s type.
Note: If the targeted element i.e., “first” is null, the “NullPointerException” is returned.
Before heading to the examples, import the below-provided packages to utilize the “Optional” class, “List”, and “Arrays”, respectively:
import java.util.List;
import java.util.Arrays;
Example 1: Applying the “findFirst()” Method Upon the Integer List in Java
In this example, the “findFirst()” method can be applied to return the first “Integer” value from the list:
public static void main(String[] args) {
List<Integer> givenlist = Arrays.asList(1, 2, 3, 4, 5);
Optional<Integer> firstValue = givenlist.stream().findFirst();
if (firstValue.isPresent()){
Integer value = firstValue.get();
System.out.println("The first value is -> "+value);
}
else {
System.out.println("Value not contained!");
}
}}
According to the above code lines:
- Firstly, create a list of “Integer” types having the stated integers.
- In the next step, associate the “findFirst()” method with the defined list to return the first integer value from the list.
- Now, apply the “isPresent()” method to ensure that the targeted value is contained against the applied method and return the corresponding value i.e., “first” via the “get()” method.
- In the other case, the “else” statement comes into effect.
Output
In this output, it can be verified that the first value from the defined list is returned appropriately.
Example 2: Applying the “findFirst()” Method Upon the String List in Java
This example applies the discussed method to return the first “String” value from the list:
public static void main(String[] args) {
List<String> givenlist = Arrays.asList("John", "Liam", "Gerard");
Optional<String> firstValue = givenlist.stream().findFirst();
if (firstValue.isPresent()){
String value = firstValue.get();
System.out.println("The first value is -> "+value);
}
else {
System.out.println("Value not contained!");
}
}}
In this code block:
- Define a “String” list comprising the stated string values.
- In the next step, likewise, apply the “findFirst()” method to return the first string value.
- After that, similarly, check if the value is contained via the “isPresent()” method and retrieve it based on that using the “get()” method.
- In the other scenario(element not found), the “else” statement will execute.
Output
Here, it can be analyzed that the first string value is returned accordingly.
Demonstration of the Encountered Exception(“NullPointerException”)
In the below-stated illustration, it can be observed that upon allocating the first list value as “null”, the “NullPointerException” is encountered upon applying the discussed method:
Conclusion
The Stream “findFirst()” method gives an “Optional” corresponding to the first element of the stream and returns the “NullPointerException” if the targeted element is “null”. This blog demonstrated the working of the Stream “findFirst()” method in Java.