2d array to ArrayList java

The following code does not compile.

Does anyone know how to convert 2d array to a list of list of Integer without a loop?

In Java, we have a Collection framework that provides functionality to store a group of objects. This is called a single-dimensional ArrayList where we can have only one element in a row. Geek but what if we want to make a multidimensional ArrayList, for this functionality for which we do have Multidimensional Collections [or Nested Collections] in Java. 

Multidimensional Collections [or Nested Collections] is a collection of groups of objects where each group can have any number of objects dynamically. Hence, here we can store any number of elements in a group whenever we want.

Illustration:

Single dimensional ArrayList : [121, 432, 12, 56, 456, 3, 1023] [Apple, Orange, Pear, Mango]

Syntax:  



ArrayList x = new ArrayList [];

Need for Multidimensional Collections

Unlike Arrays, we are not bound with the size of any row in Multidimensional collections. Therefore, if we want to use a Multidimensional architecture where we can create any number of objects dynamically in a row, then we should go for Multidimensional collections in java.

Syntax: Multidimensional Collections

ArrayList a = new ArrayList[];

Illustration: 

Multidimensional ArrayList: [[3, 4], [12, 13, 14, 15], [22, 23, 24], [33]]

 Let us quickly peek onto add[] method for multidimensional ArrayList which are as follows:

  • boolean add[ ArrayList e]: It is used to insert elements in the specified collection.
  • void add[ int index, ArrayList e]: It is used to insert the elements at the specified position in a Collection.

Example 1:

    static List create2DArrayList[]

        ArrayList x

            = new ArrayList[];

        x.add[new ArrayList[]];

            new ArrayList[Arrays.asList[3, 4, 6]]];

        x.add[2, new ArrayList[Arrays.asList[3, 84]]];

        x.add[new ArrayList[

            Arrays.asList[83, 6684, 776]]];

        x.add[new ArrayList[Arrays.asList[8]]];

        x.get[4].addAll[Arrays.asList[9, 10, 11]];

        x.get[1].addAll[3, Arrays.asList[22, 1000]];

    public static void main[String args[]]

        System.out.println["2D ArrayList :"];

        System.out.println[create2DArrayList[]];

Output 2D ArrayList : [[3], [366, 3, 4, 22, 1000, 6, 576], [3, 84], [83, 6684, 776], [8, 9, 10, 11]]

 Now let us see the same implementation of multidimensional LinkedHashSet and in order to show how it behaves differently. Similarly, we can implement any other Collection as Multidimensional Collection as depicted below: 

Syntax: 

HashSet< HashSet > a = new HashSet< HashSet >[];

Note: LinkedHashSet class contains unique elements & maintains insertion order. Therefore, in Multidimensional LinkedHashSet uniqueness will be maintained inside rows also.

Example 2:

    static Set create2DLinkedHashSet[]

        LinkedHashSet x

            = new LinkedHashSet[];

        x.add[new LinkedHashSet[

            Arrays.asList["Apple", "Orange"]]];

        x.add[new LinkedHashSet[Arrays.asList[

            "Tea", "Coffee", "Milk", "Coffee", "Water"]]];

        x.add[new LinkedHashSet[

            Arrays.asList["Tomato", "Potato", "Onion"]]];

        x.add[new LinkedHashSet[

            Arrays.asList["Tomato", "Potato", "Onion"]]];

    public static void main[String[] args]

        System.out.println["2D LinkedHashSet :"];

        System.out.println[create2DLinkedHashSet[]];

Output 2D LinkedHashSet : [[Apple, Orange], [Tea, Coffee, Milk, Water], [Tomato, Potato, Onion]]


Article Tags :

Practice Tags :

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.

import java.util.ArrayList;

    public static void main[String[] args]

        List list = Arrays.asList[

        int[][] arr = list.stream[]

                .map[l -> l.stream[].mapToInt[Integer::intValue].toArray[]]

        System.out.println[Arrays.deepToString[arr]];

Download  Run Code

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:

import java.util.ArrayList;

    public static void main[String[] args]

        List list = Arrays.asList[

                Arrays.asList["A", "B", "C"],

        String[][] arr = list.stream[]

                .map[l -> l.stream[].toArray[String[]::new]]

                .toArray[String[][]::new];

        System.out.println[Arrays.deepToString[arr]];

Download  Run Code

Output:
[[A, B, C], [B, D], [E, F]]

 
Here’s an alternative version of the above code that uses List’s toArray[] method:

    public static void main[String[] args]

        List list = Arrays.asList[

                Arrays.asList["A", "B", "C"],

        String[][] arr = list.stream[]

                .map[l -> l.toArray[new String[l.size[]]]]

                .toArray[String[][]::new];

        System.out.println[Arrays.deepToString[arr]];

Download  Run Code

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.

    public static void main[String[] args]

        List list = Arrays.asList[

                Arrays.asList["A", "B", "C"],

        String[][] arr = new String[list.size[]][];

        for [List l: list] {

            arr[i++] = l.toArray[new String[l.size[]]];

        System.out.println[Arrays.deepToString[arr]];

Download  Run Code

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.

Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and help us grow. Happy coding 🙂


Video liên quan

Chủ Đề