Thursday, September 4, 2025
HomeLanguagesJavaIntStream findAny() with examples

IntStream findAny() with examples

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

Syntax :

OptionalInt findAny()

Where, OptionalInt is a container object which
may or may not contain a non-null value 
and the function returns an OptionalInt describing some element of
this stream, or an empty OptionalInt 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 Integer Stream.




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


Output :

16
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS