IntStream iterate in steps

前端 未结 7 1522
悲&欢浪女
悲&欢浪女 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:32

    More generic solution:

    LongStream.range(0L, (to - from) / step) // +1 depends on inclusve or exclusive
                    .mapToObj(i -> (from + i * step))
                    .collect(Collectors.toList());
    

提交回复
热议问题