Limit a stream by a predicate

前端 未结 19 3396
别跟我提以往
别跟我提以往 2020-11-21 22:54

Is there a Java 8 stream operation that limits a (potentially infinite) Stream until the first element fails to match a predicate?

In Java 9 we can use

19条回答
  •  萌比男神i
    2020-11-21 23:45

    Go to get library AbacusUtil. It provides the exact API you want and more:

    IntStream.iterate(1, n -> n + 1).takeWhile(n -> n < 10).forEach(System.out::println);
    

    Declaration: I'm the developer of AbacusUtil.

提交回复
热议问题