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

前端 未结 4 852
不知归路
不知归路 2021-01-07 16:14
List integer = Stream.generate(new Supplier() {
    int i = 0 ;

    @Override
    public Integer get() {
        return ++i;
    }
}).         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-01-07 17:07

    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.

提交回复
热议问题