This post will discuss different ways to initialize a List of Lists in Java.
1. Using List.add()
method
You can declare a List of Lists in Java, using the following syntax. It uses the new operator to instantiate the list by allocating memory and returning a reference to that memory.
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.ArrayList; import java.util.List; class Main { public static void main(String[] args) { List<List<Integer>> lists = new ArrayList<>(); System.out.println(lists); } } |
Output:
[]
To add elements to it, call the add()
method. Since each element of a List of Lists is a List itself, pass the new List instance to the add()
method. Be extra careful not to pass the same mutable instance of a list to the add()
method again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.ArrayList; import java.util.List; class Main { public static void main(String[] args) { List<List<Integer>> lists = new ArrayList<>(); int M = 3; for (int i = 0; i < M; i++) { lists.add(new ArrayList<>()); } lists.forEach(System.out::println); } } |
Output:
[]
[]
[]
You can easily add lists of a specific size, initialized by nulls or with some desired value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.ArrayList; import java.util.Collections; import java.util.List; class Main { public static void main(String[] args) { List<List<String>> lists = new ArrayList<>(); int M = 3; int N = 3; for (int i = 0; i < M; i++) { lists.add(new ArrayList<>(Collections.nCopies(N, null))); } lists.forEach(System.out::println); } } |
Output:
[null, null, null]
[null, null, null]
[null, null, null]
2. Using Arrays.asList()
method
You can initialize a List of Lists in a single line in the following manner using the Arrays.asList()
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<List<Integer>> lists = Arrays.asList(Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6), Arrays.asList(6, 7, 8)); lists.forEach(System.out::println); } } |
Output:
[1, 2, 3]
[4, 5, 6]
[6, 7, 8]
With Java 9 onwards, you can use the static factory method List.of()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.List; class Main { public static void main(String[] args) { List<List<Integer>> lists = List.of( List.of(1, 2, 3), List.of(4, 5, 6), List.of(6, 7, 8) ); lists.forEach(System.out::println); } } |
Output:
[1, 2, 3]
[4, 5, 6]
[6, 7, 8]
Note that both Arrays.asList()
and List.of()
method creates an unmodifiable instance of the list. If you need a mutable instance, wrap each list instance using the ArrayList
constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.ArrayList; import java.util.List; class Main { public static void main(String[] args) { List<List<Integer>> lists = new ArrayList<>(List.of( new ArrayList<>(List.of(1, 2, 3)), new ArrayList<>(List.of(4, 5, 6)), new ArrayList<>(List.of(6, 7, 8)) )); lists.forEach(System.out::println); } } |
Output:
[1, 2, 3]
[4, 5, 6]
[6, 7, 8]
3. Using IntStream
Finally, with Java 8 you can use IntStream
to initialize a List of Lists as demonstrated below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<List<Integer>> lists = IntStream.rangeClosed(1, 3).boxed() .map(i -> Collections.nCopies(4, i)) .collect(Collectors.toList()); lists.forEach(System.out::println); } } |
Output:
[1, 1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
That’s all about initializing a List of Lists in Java.