Generate a List of sequential integers in Java
This post will discuss how to generate a list of sequential integers in Java. The solution should create a list with ranges of numbers between 1 and n.
1. Using IntStream.range() method
In Java 8 or later, this can be easily done using Streams without looping or using third-party libraries. The idea is to use the IntStream.range(…) method to generate a stream of increasing integers between the specified indices. To get a list of integers, you can box the primitive int stream and collect the stream elements into a list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { int n = 5; List<Integer> list = IntStream.range(1, n + 1) .boxed() .collect(Collectors.toList()); System.out.println(list); } } |
Output:
[1, 2, 3, 4, 5]
2. Using IntStream.iterate() method
Another alternative is to use the IntStream.iterate(…) method to get an infinite sequential ordered IntStream. It is produced by applying a function f to an initial element x, thereby producing a Stream consisting of x, f(x), f(f(x)), etc.
Here’s complete usage of this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { int n = 5; List<Integer> list = IntStream.iterate(1, i -> i + 1) .limit(n) .boxed() .collect(Collectors.toList()); System.out.println(list); } } |
Output:
[1, 2, 3, 4, 5]
The above solution restricts the number of elements in the infinite stream to n using the limit() method. A better way to do it in Java 9 and above is with the overloaded 3-arg IntStream.iterate(…) method, which returns a finite sequential ordered IntStream, terminating on satisfying the specified predicate.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { int n = 5; List<Integer> list = IntStream.iterate(1, i -> i <= n, i -> i + 1) .boxed() .collect(Collectors.toList()); System.out.println(list); } } |
Output:
[1, 2, 3, 4, 5]
3. Using Guava Ranges
Another plausible solution is to use Guava Ranges to create a sorted set of contiguous values (ContiguousSet) with the elements of a range of a discrete domain. To get a list, call the asList() method on the sorted set.
Its usage is demonstrated below.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import com.google.common.collect.ContiguousSet; import com.google.common.collect.DiscreteDomain; import com.google.common.collect.Range; import java.util.List; public class Main { public static void main(String[] args) { int n = 5; List<Integer> list = ContiguousSet.create(Range.closed(1, n), DiscreteDomain.integers()).asList(); System.out.println(list); } } |
Output:
[1, 2, 3, 4, 5]
That’s all about generating a list of sequential integers in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)