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.
import java.util.ArrayList; public static void main(String[] args) List<List<Integer>> lists = new ArrayList<>(); System.out.println(lists); |
Download Run Code
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.
import java.util.ArrayList; public static void main(String[] args) List<List<Integer>> lists = new ArrayList<>(); for (int i = 0; i < M; i++) { lists.add(new ArrayList<>()); lists.forEach(System.out::println); |
Download Run Code
Output:
[]
[]
[]
You can easily add lists of a specific size, initialized by nulls or with some desired value.
import java.util.ArrayList; import java.util.Collections; public static void main(String[] args) List<List<String>> lists = new ArrayList<>(); for (int i = 0; i < M; i++) { lists.add(new ArrayList<>(Collections.nCopies(N, null))); lists.forEach(System.out::println); |
Download Run Code
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:
public static void main(String[] args) List<List<Integer>> lists = Arrays.asList(Arrays.asList(1, 2, 3), lists.forEach(System.out::println); |
Download Run Code
Output:
[1, 2, 3]
[4, 5, 6]
[6, 7, 8]
With Java 9 onwards, you can use the static factory method List.of().
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); |
Download Run Code
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.
import java.util.ArrayList; 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); |
Download Run Code
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:
import java.util.Collections; import java.util.stream.Collectors; import java.util.stream.IntStream; 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); |
Download Run Code
Output:
[1, 1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
That’s all about initializing a List of Lists in Java.