Create a fixed-size List in Java
This post will discuss how to create a fixed-size List in Java.
1. Using Array.asList() method
To get a fixed-size list, you can simply create and pass the corresponding type array to the Array.asList() method. This will result in a fixed-size list that is backed by the specified array. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { int size = 10; List<Integer> list = Arrays.asList(new Integer[size]); System.out.println(list); } } |
Output:
[null, null, null, null, null, null, null, null, null, null]
Note that any structural changes made to the list (that change the size of the list) throw java.lang.UnsupportedOperationException. To add/remove elements from the list, you can wrap the fixed-size list using the ArrayList constructor. For instance,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { int size = 10; List<Integer> list = new ArrayList<>(Arrays.asList(new Integer[size])); list.remove(1); System.out.println(list.size()); } } |
Output:
9
2. Using Stream API
Here’s a similar approach using the Stream API. It takes advantage of the Arrays.stream() method, which can accept a primitive array and returns a sequential IntStream.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { int size = 10; List<Integer> list = Arrays.stream(new int[size]) .boxed() .collect(Collectors.toList()); System.out.println(list); } } |
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
To get an unmodifiable list, you can use the Collectors.toUnmodifiableList() collector.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { int size = 10; List<Integer> list = Arrays.stream(new int[size]) .boxed() .collect(Collectors.toUnmodifiableList()); System.out.println(list); } } |
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
3. Using Collections.nCopies() method
Alternatively, you can use the Collections.nCopies() method to get an immutable list consisting of specified copies of the supplied item. Be careful using this method on mutable objects, as the same instance of an object will be copied to all the available slots.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Collections; import java.util.List; class Main { public static void main(String[] args) { int n = 10; List<Integer> list = Collections.nCopies(n, 0); System.out.println(list); } } |
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
The above code also throws java.lang.UnsupportedOperationException on adding or removing elements from it. To facilitate structural changes to the list, convert the list into an ArrayList.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.ArrayList; import java.util.Collections; import java.util.List; class Main { public static void main(String[] args) { int size = 10; List<Integer> list = new ArrayList<>(Collections.nCopies(size, 0)); System.out.println(list); } } |
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
4. Using IntStream
The idea here is to generate an infinite sequential ordered IntStream, limit it with the required size, and map it to some default value. Finally, collect the elements into a list.
|
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 = 10; List<Integer> list = IntStream.iterate(0, i -> i + 1) .limit(size) .boxed() .map(i -> 0) .collect(Collectors.toList()); System.out.println(list); } } |
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Java 9 offers a more flexible version of the IntStream.iterate() method that can terminate the stream without the need for the limit() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { int size = 10; List<Integer> list = IntStream.iterate(1, i -> i <= size, i -> i + 1) .boxed() .map(i -> 0) .collect(Collectors.toList()); System.out.println(list); } } |
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
That’s all about creating a fixed-size List 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 :)