Equivalent of Scala dropWhile

后端 未结 2 1808
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 16:21

I\'m struggling to find a way to skip some elements at the beginning of a stream depending on a predicate.

Something like this:

dropWhile( n -> n          


        
2条回答
  •  一个人的身影
    2020-11-28 17:12

    Unfortunately, the only way to do that with Java 8 is with the solution provided by Holger.

    However, the operation dropWhile(predicate) has been added to Java 9 so starting from JDK 9, you can simply have:

    Stream.of(0, 1, 2, 3, 0, 1, 2, 3, 4).dropWhile(n -> n < 3).forEach(System.out::println);
    

提交回复
热议问题