I've always learned that Java 8 streams are lazy. As such, I would expect that:
Optional<Integer> intOpt = Stream
.of(1, 2, 3)
.flatMap(i -> Stream.of(4, 5, 6))
.filter(i -> {System.out.println(i); return true;})
.findAny();
applies the filter only once. However, the above program outputs:
4
5
6
and the value of intOpt is an Optional[4]. If the filter performs some expensive function, then the above code will perform this expensive function more times than necessary.
Streams are not so lazy after all!
A quick Google search shows that I'm not the only one surprised by this semantics.