HomeLanguagesJavaDoubleStream findAny() with examples

DoubleStream findAny() with examples

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.

Syntax :

OptionalDouble findAny()

Parameters :

  1. 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.




// Java code for DoubleStream findAny()
// which returns an OptionalDouble describing
// some element of the stream, or an
// empty OptionalDouble if the stream is empty.
import java.util.*;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an DoubleStream
        DoubleStream stream = DoubleStream.of(6.3, 7.4, 8.5, 9.6);
  
        // Using DoubleStream findAny() to return
        // an OptionalDouble describing some element
        // of the stream
        OptionalDouble answer = stream.findAny();
  
        // if the stream is empty, an empty
        // OptionalDouble is returned.
        if (answer.isPresent()) {
            System.out.println(answer.getAsDouble());
        }
        else {
            System.out.println("no value");
        }
    }
}


Output :

6.3

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.




// Java code for DoubleStream findAny()
// which returns an OptionalDouble describing
// some element of the stream, or an
// empty OptionalDouble if the stream is empty.
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an DoubleStream
        DoubleStream stream = DoubleStream.of(4.4, 5.6,
                    8.4, 10.2, 12.0, 16.0).parallel();
  
        // Using DoubleStream findAny().
        // Executing the source code multiple times
        // may not return the same result.
        // Every time you may get a different
        // Double value which is divisible by 4.
        stream = stream.filter(num -> num % 4 == 0);
  
        OptionalDouble answer = stream.findAny();
        if (answer.isPresent()) {
            System.out.println(answer.getAsDouble());
        }
    }
}


Output :

16.0
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32675 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7043 POSTS0 COMMENTS
Nicole Veronica
12182 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12277 POSTS0 COMMENTS
Shaida Kate Naidoo
7166 POSTS0 COMMENTS
Ted Musemwa
7436 POSTS0 COMMENTS
Thapelo Manthata
7140 POSTS0 COMMENTS
Umr Jansen
7133 POSTS0 COMMENTS