Leslie.Lee
Leslie.Lee

Reputation: 27

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

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 get this?

Upvotes: 1

Views: 163

Answers (1)

Ousmane D.
Ousmane D.

Reputation: 56463

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());

Upvotes: 9

Related Questions