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

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button