Is the skip() method a short circuiting-operation?

可紊 提交于 2021-02-18 21:12:40

问题


I am reading about Java streams' short-circuiting operations and found in some articles that skip() is a short-circuiting operation.

In another article they didn't mention skip() as a short-circuiting operation.

Now I am confused; is skip() a short-circuiting operation or not?


回答1:


From the java doc under the "Stream operations and pipelines" section :

An intermediate operation is short-circuiting if, when presented with infinite input, it may produce a finite stream as a result. A terminal operation is short-circuiting if, when presented with infinite input, it may terminate in finite time.

Emphasis mine.

if you were to call skip on an infinite input it won't produce a finite stream hence not a short-circuiting operation.

The only short-circuiting intermediate operation in JDK8 is limit as it allows computations on infinite streams to complete in finite time.

Example:

if you were to execute this program with the use of skip:

String[] skip = Stream.generate(() -> "test") // returns an infinite stream
                      .skip(20) 
                      .toArray(String[]::new);

it will not produce a finite stream hence you would eventually end up with something along the lines of "java.lang.OutOfMemoryError: Java heap space".

whereas if you were to execute this program with the use of limit, it will cause the computation to finish in a finite time:

String[] limit = Stream.generate(() -> "test") // returns an infinite stream
                       .limit(20)
                       .toArray(String[]::new);



回答2:


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



来源:https://stackoverflow.com/questions/51571912/is-the-skip-method-a-short-circuiting-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!