Convert a List of Lists to a 2D array in Java
This post will discuss how to convert a List of Lists to a two-dimensional primitive or Object array in Java.
There is no straightforward way to convert a List of Lists to a two-dimensional array. However, introduction of Stream API in Java 8 made this task a little simpler:
1. Stream API
The standard solution to convert a List of Lists to a two-dimensional primitive array in Java is using Stream API. The idea is to get a stream of the list of lists and use map() to replace each of the nested lists with the corresponding single-dimensional array. Then, finally call toArray() with a generator to produce the two-dimensional primitive array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { // Input list List<List<Integer>> list = Arrays.asList( Arrays.asList(1, 3, 2), Arrays.asList(1, 2, 2), Arrays.asList(1, 2) ); int[][] arr = list.stream() .map(l -> l.stream().mapToInt(Integer::intValue).toArray()) .toArray(int[][]::new); System.out.println(Arrays.deepToString(arr)); } } |
Output:
[[1, 3, 2], [1, 2, 2], [1, 2]]
A similar idea can be applied to the list of lists of Wrapper class to get the two-dimensional array, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { // Input list List<List<String>> list = Arrays.asList( Arrays.asList("A", "B", "C"), Arrays.asList("B", "D"), Arrays.asList("E", "F") ); String[][] arr = list.stream() .map(l -> l.stream().toArray(String[]::new)) .toArray(String[][]::new); System.out.println(Arrays.deepToString(arr)); } } |
Output:
[[A, B, C], [B, D], [E, F]]
Here’s an alternative version of the above code that uses List’s toArray() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { // Input list List<List<String>> list = Arrays.asList( Arrays.asList("A", "B", "C"), Arrays.asList("B", "D"), Arrays.asList("E", "F") ); String[][] arr = list.stream() .map(l -> l.toArray(new String[l.size()])) .toArray(String[][]::new); System.out.println(Arrays.deepToString(arr)); } } |
Output:
[[A, B, C], [B, D], [E, F]]
2. Using Loops
Before Java 8, you can create a two-dimensional array of the same size as that of the given list and use a for-loop to fill each array row with the results of the calling toArray() method upon each of the nested lists.
|
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.Arrays; import java.util.List; class Main { public static void main(String[] args) { // Input list List<List<String>> list = Arrays.asList( Arrays.asList("A", "B", "C"), Arrays.asList("B", "D"), Arrays.asList("E", "F") ); String[][] arr = new String[list.size()][]; int i = 0; for (List<String> l: list) { arr[i++] = l.toArray(new String[l.size()]); } System.out.println(Arrays.deepToString(arr)); } } |
Output:
[[A, B, C], [B, D], [E, F]]
That’s all about converting a List of Lists to a two-dimensional array 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 :)