IntStream iterate in steps

前端 未结 7 1529
悲&欢浪女
悲&欢浪女 2020-12-03 07:41

How do you iterate through a range of numbers (0-100) in steps(3) with IntStream?

I tried iterate, but this never stops executing.

IntS         


        
7条回答
  •  伪装坚强ぢ
    2020-12-03 08:37

    Elegant Solution:

    IntStream.iterate(0, n -> n < 100, n -> n + 3).forEach(System.out::println)
    

    Stream.iterate() supports a hasNext() predicate (added in Java 9) which can be used to limit the stream in more natural way.

提交回复
热议问题