LongStream findFirst() in Java
LongStream findFirst() returns an OptionalLong (a container object which may or may not contain a non-null value) describing the first element of this stream, or an empty OptionalLong if the stream is empty
Syntax :
OptionalLong findFirst()
Parameters :
- OptionalLong : A container object which may or may not contain a non-null value.
Return Value : The function returns an OptionalLong describing the first element of this stream, or an empty OptionalLong if the stream is empty.
Note : findAny() is a terminal-short-circuiting operation of Stream interface. This method returns any first element satisfying the intermediate operations.
Example 1 : findFirst() method on Long Stream.
// Java code for LongStream findFirst() // which returns an OptionalLong describing // first element of the stream, or an // empty OptionalLong if the stream is empty. import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // Creating an LongStream LongStream stream = LongStream.of(6L, 7L, 8L, 9L); // Using LongStream findFirst() to return // an OptionalLong describing first element // of the stream OptionalLong answer = stream.findFirst(); // if the stream is empty, an empty // OptionalLong is returned. if (answer.isPresent()) System.out.println(answer.getAsLong()); 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 LongStream findFirst() // which returns an OptionalLong describing // first element of the stream, or an // empty OptionalLong if the stream is empty. import java.util.OptionalLong; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // Creating an LongStream LongStream stream = LongStream.of(4L, 5L, 8L, 10L, 12L, 16L) .parallel(); // Using LongStream findFirst(). // Executing the source code multiple times // must return the same result. // Every time you will get the same // value which is divisible by 4. stream = stream.filter(num -> num % 4 == 0); OptionalLong answer = stream.findFirst(); if (answer.isPresent()) System.out.println(answer.getAsLong()); } } |
Output :
4
Recommended Posts:
- IntStream findFirst() in Java
- Stream findFirst() in Java with examples
- LongStream of() in Java
- LongStream sum() in Java
- LongStream min() in Java with examples
- LongStream boxed() in Java
- LongStream max() in Java with examples
- LongStream range() in Java
- LongStream asDoubleStream() in Java
- LongStream builder() in Java
- LongStream mapToObj() in Java
- LongStream toArray() in Java
- LongStream limit() in Java
- LongStream mapToDouble() in Java
- LongStream parallel() in Java
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.


