Multidimensional Collections in Java

In Java we have Collection framework which provides functionality to store group of objects.
Syntax :

ArrayList <Object> x= new ArrayList <Object>();

Examples:

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

This is called as single dimensional ArrayList where we can have only one element in a row.
But what if we want to make multidimensional ArrayList ? For this functionality we have Multidimensional Collections (or Nested Collections) in java.



What is Multidimensional Collections in java?
Multidimensional Collections (or Nested Collections) is a collection of group 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.

Need for Multidimensional Collections in java?
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.

Multidimensional_Collections_in_Java

Syntax for Multidimensional Collections :

ArrayList<ArrayList<Object>> a = new ArrayList<ArrayList<Object>>();

Examples:

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

Add Method for Multidimensional ArrayList in Java:

boolean add( ArrayList<Object> e) : It is used to insert elements in the specified collection.
void add( int index, ArrayList<Object> e) : It is used to insert the elements at specified
position in a Collection.

Below is implementation of Multidimensional ArrayList in Java :

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Java program for Multidimensional ArrayList */
  
import java.util.*;
  
class MultidimensionalArrayList {
  
    /*function for creating and returning 2D ArrayList*/
    static List create2DArrayList()
    {
        /*Declaring 2D ArrayList*/
        ArrayList<ArrayList<Integer> > x
            = new ArrayList<ArrayList<Integer> >();
  
        /*one space allocated for 0th row*/
        x.add(new ArrayList<Integer>());
  
        /*Adding 3 to 0th row created above x(0, 0)*/
        x.get(0).add(0, 3);
  
        /*Creating 1st row and adding values 
       (another way for adding values in 2D collections)*/
        x.add(new ArrayList<Integer>(Arrays.asList(3, 4, 6)));
  
        /*Add 366 to 1st row 0th column x(1, 0)*/
        x.get(1).add(0, 366);
  
        /*Add 576 to 1st row 4th column x(1, 4)*/
        x.get(1).add(4, 576);
  
        /*Adding values to 2nd row*/
        x.add(2, new ArrayList<>(Arrays.asList(3, 84)));
  
        /*Adding values to 3rd row*/
        x.add(new ArrayList<Integer>(Arrays.asList(83, 6684, 776)));
  
        /*Adding values to 4th row*/
        x.add(new ArrayList<>(Arrays.asList(8)));
  
        return x;
    }
  
    public static void main(String args[])
    {
        System.out.println("2D ArrayList :");
  
        /*Printing 2D ArrayList*/
        System.out.println(create2DArrayList());
    }
}

chevron_right


Output:

2D ArrayList :
[[3], [366, 3, 4, 6, 576], [3, 84], [83, 6684, 776], [8]]

Example 2: Now let’s see the implementation of Multidimensional LinkedHashSet in Java. Note: LinkedHashSet class contains unique elements & maintains insertion order. Therefore, in Multidimensional LinkedHashSet uniqueness will be maintained inside rows also.

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Java program for Multidimensional LinkedHashSet */
  
import java.util.*;
  
class MultidimensionalLinkedHashSet {
  
    /*function for creating and returning 2D LinkedHashSet*/
    static Set create2DLinkedHashSet()
    {
        /*Declaring 2D LinkedHashSet*/
        LinkedHashSet<LinkedHashSet<String> > x
            = new LinkedHashSet<LinkedHashSet<String> >();
  
        /*Creating 1st row */
        x.add(new LinkedHashSet<String>(
            Arrays.asList("Apple", "Orange")));
  
        // Creating 2nd row, here "Coffee" will be
        // considered as only one object
        // to maintain uniqueness
        x.add(new LinkedHashSet<String>(
            Arrays.asList("Tea",
                          "Coffee",
                          "Milk",
                          "Coffee",
                          "Water")));
  
        /* Creating 3rd row */
        x.add(new LinkedHashSet<String>(
            Arrays.asList("Tomato",
                          "Potato",
                          "Onion")));
  
        // Creating 4th row but it will not
        // be added as it contains the same items as 3rd row
        // (LinkedHashSet inserts only unique items)
        x.add(new LinkedHashSet<String>(
            Arrays.asList("Tomato", "Potato", "Onion")));
  
        return x;
    }
  
    public static void main(String[] args)
    {
        System.out.println("2D LinkedHashSet :");
  
        /*Printing 2D LinkedHashSet*/
        System.out.println(create2DLinkedHashSet());
    }
}

chevron_right


Output:

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

Similarly, we can implement any other Collection as Multidimensional Collection .
Example :

HashSet< HashSet<Object> > a = new HashSet< HashSet<Object> >();



My Personal Notes arrow_drop_up

I am passionate about coding and my area of interest are Data Structures & Algorithms My proficiency is in Java and I like to build web applications

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.