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.
package com.logicbig.example;
import java.util.OptionalInt;
import java.util.stream.IntStream;
public class FindAnyExample {
public static void main (String[] args) {
IntStream stream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.parallel();
stream = stream.filter(i -> i % 2 == 0);
OptionalInt opt = stream.findAny();
if (opt.isPresent()) {
System.out.println(opt.getAsInt());
}
}
}
Output
6
Original Post