IntStream findFirst() returns an OptionalInt (a container object which may or may not contain a non-null value) describing the first element of this stream, or an empty OptionalInt if the stream is empty
Syntax :
OptionalInt findFirst() Where, OptionalInt is a container object which may or may not contain a non-null value and the function returns an OptionalInt describing the first element of this stream, or an empty OptionalInt if the stream is empty.
Note : findFirst() is a terminal-short-circuiting operation of Stream interface. This method returns any first element satisfying the intermediate operations.
Example 1 : findFirst() method on Integer Stream.
// Java code for IntStream findFirst() // which returns an OptionalInt describing // first element of the stream, or an // empty OptionalInt if the stream is empty. import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating an IntStream IntStream stream = IntStream.of(6, 7, 8, 9); // Using IntStream findFirst() to return // an OptionalInt describing first element // of the stream OptionalInt answer = stream.findFirst(); // if the stream is empty, an empty // OptionalInt is returned. if (answer.isPresent()) System.out.println(answer.getAsInt()); else System.out.println("no value"); } } |
Output :
6
Note : If the stream has no encounter order, then any element may be returned.
Example 2 : findFirst() method to return the first element which is divisible by 4.
// Java code for IntStream findFirst() // which returns an OptionalInt describing // first element of the stream, or an // empty OptionalInt if the stream is empty. import java.util.OptionalInt; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating an IntStream IntStream stream = IntStream.of(4, 5, 8, 10, 12, 16) .parallel(); // Using IntStream findFirst(). // Executing the source code multiple times // must return the same result. // Every time you will get the same // Integer which is divisible by 4. stream = stream.filter(num -> num % 4 == 0); OptionalInt answer = stream.findFirst(); if (answer.isPresent()) System.out.println(answer.getAsInt()); } } |
Output :
4
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Stream findFirst() in Java with examples
- LongStream findFirst() in Java
- DoubleStream findFirst() with examples
- java.util.stream.IntStream/LongStream | Search an element
- IntStream iterator() in Java
- IntStream rangeClosed() in Java
- IntStream map(IntUnaryOperator mapper) in Java
- IntStream parallel() in Java
- IntStream empty() in Java with examples
- IntStream distinct() in Java with examples
- IntStream peek() in Java with examples
- IntStream average() in Java with Examples
- IntStream max() in Java with examples
- IntStream min() in Java with Examples
- IntStream count() in Java with examples
- IntStream skip() in Java
- IntStream filter() in Java with examples
- IntStream concat() in Java
- IntStream anyMatch() in Java with examples
- IntStream allMatch() in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

