How Can I use Java Stream To Build a List And The Integer Between a and b

前端 未结 1 318
伪装坚强ぢ
伪装坚强ぢ 2021-01-24 11:48

Say I have two variables : a = 5, b = 8,

And I want :

Arrays.asList(5, 6, 7, 8)

How Can I Use Java stream To

相关标签:
1条回答
  • 2021-01-24 12:25

    you can use IntStream.rangeClosed to generate the numbers and collect into a list.

    List<Integer> result = IntStream.rangeClosed(a, b)
                                    .boxed()
                                    .collect(Collectors.toList());
    
    0 讨论(0)
提交回复
热议问题