To create a range in java we can use the following from the java.util.stream package:

  • IntStream.range(int startInclusive, int endExclusive)
  • IntStream.rangeClosed(int startInclusive, int endInclusive)
  • LongStream.range(int startInclusive, int endExclusive)
  • LongStream.rangeClosed(int startInclusive, int endInclusive)

IntStream.range Method Example

IntStream.range(1, 10).forEach(item -> System.out.println(item));

Output

1
2
3
4
5
6
7
8
9

IntStream.rangeClosed Method Example

IntStream.rangeClosed(1, 10).forEach(item -> System.out.println(item));

Output

1
2
3
4
5
6
7
8
9
10