DoubleStream findAny() returns an OptionalDouble (a container object which may or may not contain a non-null value) describing some element of the stream, or an empty OptionalDouble if the stream is empty.
- OptionalDouble : A container object which may or may not contain a non-null value.
Return Value : The function returns an OptionalDouble describing some element of this stream, or an empty OptionalDouble 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 Double Stream.
Note : The behavior of DoubleStream 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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.