Initialize a list in Java in a single line with specific value
This post will discuss how to initialize a list in Java in a single line with the specified value.
A naive solution is to call the List.add() method n times in a loop, where n is the specified size of the list. This works fine, but there are better ways, as discussed below:
1. Using Collections.nCopies() method
Java collection framework has provided the Collections.nCopies() method, which returns an immutable list consisting of the specified copies of the specified object. The object allocated by this method holds a single reference to the specified object; hence its memory consumption is very less.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.ArrayList; import java.util.Collections; import java.util.List; class Main { public static void main(String[] args) { int size = 5; int val = 1; List<Integer> ints = Collections.nCopies(size, val); System.out.println(ints); // [1, 1, 1, 1, 1] } } |
2. Using Java 8 Stream
We can also use Java 8 Stream for this. The idea is to use the Stream.generate() method, which takes a Supplier. In the following example, we have created an infinite stream of empty character sequence, which is limited by using the limit() method. Finally, each element is mapped to the specified value and collected in an immutable list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static<T> List<T> generate(int size, T val) { return Stream.generate(String::new) .limit(size) .map(s -> val) .collect(Collectors.toList()); } public static void main(String[] args) { int size = 5; int val = 1; List<Integer> ints = generate(size, val); System.out.println(ints); // [1, 1, 1, 1, 1] } } |
The above approach can work on any object or a primitive value. If we have a list of Integer, we can do something like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { int size = 5; int val = 1; List<Integer> ints = IntStream.generate(() -> val) .limit(size) .boxed() .collect(Collectors.toList()); System.out.println(ints); // [1, 1, 1, 1, 1] } } |
[1, 1, 1, 1, 1]
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.ArrayList; import java.util.List; class Main { public static void main(String[] args) { int size = 5; int val = 1; List<Integer> ints = new ArrayList<>(); for (long count = size; count > 0; count--) { ints.add(val); } System.out.println(ints); // [1, 1, 1, 1, 1] } } |
[1, 1, 1, 1, 1]
3. Using intermediate array
Well, this is not a single liner, but worth mentioning. The idea is to create an array of the specified size and use Arrays.fill() to initialize it by the given value. Then we pass this array to the Arrays.asList() method to get an immutable list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { int size = 5; int val = 1; Integer[] data = new Integer[size]; Arrays.fill(data, val); List<Integer> ints = Arrays.asList(data); System.out.println(ints); // [1, 1, 1, 1, 1] } } |
Here’s how we can do the same in a single line using Java 8 Stream, which can work on any object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static<T> List<T> generate(int size, T val) { return IntStream.of(new int[size]) .mapToObj(i -> val) .collect(Collectors.toList()); } public static void main(String[] args) { int size = 5; int val = 1; List<Integer> ints = generate(size, val); System.out.println(ints); // [1, 1, 1, 1, 1] } } |
We can also use Arrays.stream() or Stream.of() with map() method.
|
1 2 3 4 5 |
public static<T> List<T> generate(int size, T val) { return Arrays.stream(new Integer[size]) // or, use `Stream.of()` .map(i -> val) .collect(Collectors.toList()); } |
Please note that all the above-mentioned methods produce an immutable list. To get a mutable instance, we need to wrap the list using the ArrayList constructor. For instance,
|
1 |
List<Integer> ints = new ArrayList(Collections.nCopies(size, val)); |
That’s all about initializing a list in Java in a single line with the specified value.
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 :)