How does short-circuit work for Java 8 Streams?

There’s a subtle difference, because anyMatch family uses a predicate, while findAny family does not. Technically findAny() looks like anyMatch(x -> true) and anyMatch(pred) looks like filter(pred).findAny(). So here we have another issue. Consider we have a simple infinite stream:

Stream<Integer> s = Stream.generate(() -> 1);

So it’s true that applying findAny() to such stream will always short-circuit and finish while applying anyMatch(pred) depends on the predicate. However let’s filter our infinite stream:

Stream<Integer> s = Stream.generate(() -> 1).filter(x -> x < 0);

Is the resulting stream infinite as well? That’s a tricky question. It actually contains no elements, but to determine this (for example, using .iterator().hasNext()) we have to check the infinite number of underlying stream elements, so this operation will never finish. I would call such stream an infinite as well. However using such stream both anyMatch and findAny will never finish:

Stream.generate(() -> 1).filter(x -> x < 0).anyMatch(x -> true);
Stream.generate(() -> 1).filter(x -> x < 0).findAny();

So findAny() is not guaranteed to finish either, it depends on the previous intermediate stream operations.

To conclude I would rate that blog-post as very misleading. In my opinion infinity stream behavior is better explained in official JavaDoc.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)