Why doesn't Stream.limit work as expected in this snippet?

时间秒杀一切 提交于 2019-12-12 07:25:25

问题


List<Integer> integer = Stream.generate(new Supplier<Integer>() {
    int i = 0 ;

    @Override
    public Integer get() {
        return ++i;
    }
}).filter(j -> j < 5)
  .limit(10)   // Note the call to limit here
  .collect(Collectors.toList());

Counter to my expectation, the collect call never returns. Setting limit before filter produces the expected result. Why?


回答1:


Since there are only 4 elements that pass the filter, limit(10) never reaches 10 elements, so the Stream pipeline keeps generating new elements and feeding them to the filter, trying to reach 10 elements that pass the filter, but since only the first 4 elements pass the filter, the processing never ends (at least until i overflows).

The Stream pipeline is not smart enough to know that no more elements can pass the filter, so it keeps processing new elements.




回答2:


Flipping the limit and the filter clauses has different behaviors.

If you put the limit first, the stream will first generate 10 integers [1..10], and then filter them leaving only those smaller than 5.

In the original ordering, with the filter applied first, integers are generated and filtered until you reach 10 elements. This isn't an infinite operator, as i in the supplier will eventually overflow, but it will take a while, especially on a slow computer, to reach MAX_INT.




回答3:


If you want to stop either if number 5 is reached or 10 elements are collected, there's Stream.takeWhile() method added in Java-9:

List<Integer> integer = Stream.generate(new Supplier<Integer>() {
    int i = 0 ;

    @Override
    public Integer get() {
        return ++i;
    }
}).takeWhile(j -> j < 5).limit(10).collect(Collectors.toList());



回答4:


It will finish, after the Supplier overflows and starts generating negative numbers. The resulting list will contain:

[1, 2, 3, 4, -2147483648, -2147483647, -2147483646, -2147483645, -2147483644, -2147483643]

The reason for this is in other answers. On my i7 machine it took 40 seconds to complete.



来源:https://stackoverflow.com/questions/34172978/why-doesnt-stream-limit-work-as-expected-in-this-snippet

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