Just want to add my two cents here, this idea in general of a short-circuiting a stream is infinitely complicated (at least to me and at least in the sense that I have to scratch my head twice usually). I will get to skip
at the end of the answer btw.
Let's take this for example:
Stream.generate(() -> Integer.MAX_VALUE);
This is an infinite stream, we can all agree on this. Let's short-circuit it via an operation that is documented to be as such (unlike skip
):
Stream.generate(() -> Integer.MAX_VALUE).anyMatch(x -> true);
This works nicely, how about adding a filter
:
Stream.generate(() -> Integer.MAX_VALUE)
.filter(x -> x < 100) // well sort of useless...
.anyMatch(x -> true);
What will happen here? Well, this never finishes, even if there is a short-circuiting operation like anyMatch
- but it's never reached to actually short-circuit anything.
On the other hand, filter
is not a short-circuiting operation, but you can make it as such (just as an example):
someList.stream()
.filter(x -> {
if(x > 3) throw AssertionError("Just because");
})
Yes, it's ugly, but it's short-circuiting... That's how we (emphases on we, since lots of people, disagree) implement short-circuiting reduce
- throw an Exception that has no stack traces.
In java-9
there was an addition of another intermediate operation that is short-circuiting: takeWhile
that acts sort of like limit
but for a certain condition.
And to be fair, the bulk of the answer about skip
was an already give by Aomine, but the most simple answer is that it is not documented as such. And in general (there are cases when documentation is corrected), but that is the number one indication you should look at. See limit
and takeWhile
for example that clearly says:
This is a short-circuiting stateful intermediate operation
limit
, here : https://mcmap.net/q/169406/-is-the-skip-method-a-short-circuiting-operation – Cloakroom