Stream findFirst() Method in Java
The Stream
findFirst()
method returns an Optional describing the 1st element of the stream, or an Optional, which has to be empty if the stream is empty.
Syntax:
Optional<T> findFirst()
Here, Optional is the container object that can or cannot fetch a non-null value. T is the type of object.
Exception:
If a null element is chosen, then the NullPointerException is thrown.
Example 1:
Observe the following program.
FileName:
FindFirstExample.java
import java.util.Optional;
import java.util.List;
import java.util.Arrays;
public class FindFirstExample
// main method
public static void main(String[] argvs)
// input list
List
lst = Arrays.asList(11, 12, 13, 14, 12, 11);
// since findFirst() returns an optional
Optional
findfirst = lst.stream().findFirst();
if (findfirst.isPresent())
Integer outcome = findfirst.get();
System.out.println(outcome); // prints 11
// control reaching here means findfirst is empty
System.out.println("no value.");
// picking the first element that is greater than 11, we
// see that the element is 12 as 12 is greater than 11 and
// is immediately next to the element 11
Optional
findfirst1 = lst.stream().filter(y -> y > 11).findFirst();
if (findfirst1.isPresent())
System.out.println(findfirst1.get()); // prints 12
// control reaching here means findfirst1 is empty
System.out.println("no value ");
Output:
Example 2:
Let's see another exmaple of the method findFirst().
FileName:
FindFirstExample1.java
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class FindFirstExample1
// main method
public static void main(String[] argvs)
// creating a list
List
lst = Arrays.asList("Linked List", "C++", "Java", "Ruby", "C", "Scala");
// chooses the first element of the stream that is not equal to C++, which is
// linked list in our case
Optional
outcome = lst.stream().filter(x -> !x.equalsIgnoreCase("C++")).findFirst();
if (outcome.isPresent())
System.out.println(outcome.get()); // Linked List
System.out.println("no value");
Output:
Linked List
Example 3: Null Exception
As discussed earlier, a NullPointerException is thrown when a null element is chosen. The following program shows the same.
FileName: FindFirstExample2.java
// important import statements
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class FindFirstExample2
// main method
public static void main(String[] argvs)
// creating a list
List lst = Arrays.asList(null, "C++", "Java", "Ruby", "C", "Scala");
// chooses the first element of the stream that is not equal to C++, which is null
// in our case
Optional outcome = lst.stream().filter(x -> !x.equalsIgnoreCase("C++")).findFirst();
if (outcome.isPresent())
// as the null element is chosen, we get the nullpointerexception
System.out.println(outcome.get());
System.out.println("no value");
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.equalsIgnoreCase(String)" because "" is null
at findFirstExample2.lambda$main$0(findFirstExample2.java:15)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:178)
at java.base/java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:1002)
at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:129)
at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:527)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:513)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:647)
at findFirstExample2.main(findFirstExample2.java:15)
Next TopicBalanced Parentheses in Java