Stream findAny() returns an Optional (a container object which may or may not contain a non-null value) describing some element of the stream, or an empty Optional if the stream is empty.
findAny() V/s findFirst() :
The findAny() method returns any element from a Stream but there might be a case where we require the first element of a filtered stream to be fetched. When the stream being worked on has a defined encounter order(the order in which the elements of a stream are processed), then findFirst() is useful which returns the first element in a Stream.
Optional<T> findAny() Where, Optional is a container object which may or may not contain a non-null value and T is the type of objects and the function returns an Optional describing some element of this stream, or an empty Optional if the stream is empty.
Exception : If the element selected is null, NullPointerException is thrown.
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.
Example 2 : findAny() function on Stream of Strings.
Note : The behavior of Stream 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 3 : findAny() method to return the elements divisible by 4, in a non-deterministic way.
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.
- IntStream findAny() with examples
- DoubleStream findAny() with examples
- LongStream findAny() with examples
- Character Stream Vs Byte Stream in Java
- Difference between Stream.of() and Arrays.stream() method in Java
- foreach() loop vs Stream foreach() vs Parallel Stream foreach()
- Stream skip() method in Java with examples
- Stream.reduce() in Java with examples
- Stream.max() method in Java with Examples
- Stream min() method in Java with Examples
- Stream generate() method in Java with examples
- Stream count() method in Java with examples
- Stream.of(T... values) in Java with examples
- Stream.of(T t) in Java with examples
- Stream peek() in Java with examples
- Stream map() in Java with examples
- Stream mapToInt() in Java with examples
- Stream mapToLong() in Java with examples
- Stream mapToDouble() in Java with examples
- Stream 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 firstname.lastname@example.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.