Friday, October 17, 2025
HomeLanguagesJavaLongStream findAny() with examples

LongStream findAny() with examples

LongStream findAny() returns an OptionalLong (a container object which may or may not contain a non-null value) describing some element of the stream, or an empty OptionalLong if the stream is empty.

Syntax :

OptionalLong findAny() 

Parameters :

  1. OptionalLong : A container object which may or may not contain a non-null value.

Return Value : The function returns an OptionalLong describing some 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. This is a short-circuit operation because it just needs ‘any’ first element to be returned and terminate the rest of the iteration.

Example 1 : findAny() method on Long Stream.




// Java code for LongStream findAny()
// which returns an OptionalLong describing
// some 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 findAny() to return
        // an OptionalLong describing some element
        // of the stream
        OptionalLong answer = stream.findAny();
  
        // 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 : The behavior of LongStream findAny() operation is explicitly non-deterministic i.e, it is free to select any element in the stream. Multiple invocations on the same source may not return the same result.

Example 2 : findAny() method to return the elements divisible by 4, in a non-deterministic way.




// Java code for LongStream findAny()
// which returns an OptionalLong describing
// some 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 findAny().
        // Executing the source code multiple times
        // may not return the same result.
        // Every time you may get a different
        // value which is divisible by 4.
        stream = stream.filter(num -> num % 4 == 0);
  
        OptionalLong answer = stream.findAny();
        if (answer.isPresent()) {
            System.out.println(answer.getAsLong());
        }
    }
}


Output :

16
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS