Convert List Of List To 2d Array Java

Convert List of List to 2d Array Java | In this post, we write a Java program to convert list of list to 2d array java.

Let us understand it through an example:-
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
listOfList = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
Now, we have a list of lists and we want to convert it into a 2d array. Let us see how to convert list list integer to 2d array Java.

Program To Convert List Of List To 2d Array Java using Stream

import java.util.Arrays;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      List<List<Integer>> listOfList = 
            Arrays.asList(Arrays.asList(7, 8, 5), 
                       Arrays.asList(4, 3, 22),
                       Arrays.asList(4, 3, 7));

      int[][] array = listOfList.stream()
                          .map(l -> l.stream()
                               .mapToInt(Integer::intValue)
                               .toArray()
                              )
                          .toArray(int[][]::new);
      
      System.out.println("2D array: " +
             Arrays.deepToString(array));
   }
}

Output:-

2D array: [ [7, 8, 5], [4, 3, 22], [4, 3, 7] ]

The explanation of the code is as follows:-
Step-1: Import arrays and list as we are working on them we might need the functions and methods in arrays and list.
Step-2:- In the main class, create the main method. In the main method create an object of list integer and add the elements to it by using arrays.asList().
Step-3:- By using stream().map() method that converts the list elements to two dimensions array and prints the same.

In the above program, we initialized list of list using the Arrays.asList() method. Let us see another program to convert list of list to 2d array Java.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      List<List<Integer>> listOfList = 
               new ArrayList<List<Integer>>();
      List<Integer> list1 = new ArrayList<>();
      list1.add(10);
      list1.add(20);
      list1.add(30);
      listOfList.add(list1);
      List<Integer> list2 = new ArrayList<>();
      list2.add(9);
      list2.add(18);
      list2.add(27);
      listOfList.add(list2);
      List<Integer> list3 = new ArrayList<>();
      list3.add(11);
      list3.add(22);
      list3.add(33);
      listOfList.add(list3);
      System.out.println("List of list = " + listOfList);
      
      int[][] array = listOfList.stream()
            .map(l -> l.stream()
                .mapToInt(Integer::intValue)
                .toArray()
                )
            .toArray(int[][]::new);

      System.out.println("2D array: " +
             Arrays.deepToString(array));
   }
}

Output:-

List of list = [ [10, 20, 30], [9, 18, 27], [11, 22, 33] ]
2D array: [ [10, 20, 30], [9, 18, 27], [11, 22, 33] ]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *