In Java we have Collection framework which provides functionality to store group of objects.
ArrayList <Object> x= new ArrayList <Object>();
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.
Syntax for Multidimensional Collections :
ArrayList<ArrayList<Object>> a = new ArrayList<ArrayList<Object>>();
Multidimensional ArrayList: [[3, 4], [12, 13, 14, 15], [22, 23, 24], ]
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 :
2D ArrayList : [, [366, 3, 4, 6, 576], [3, 84], [83, 6684, 776], ]
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.
2D LinkedHashSet : [[Apple, Orange], [Tea, Coffee, Milk, Water], [Tomato, Potato, Onion]]
Similarly, we can implement any other Collection as Multidimensional Collection .
HashSet< HashSet<Object> > a = new HashSet< HashSet<Object> >();
- Difference between Traditional Collections and Concurrent Collections in java
- Multidimensional Arrays in Java
- Array Declarations in Java (Single and Multidimensional)
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.frequency() in Java with Examples
- Collections in Java
- Collections.nCopies() in Java
- Need of Concurrent Collections in java
- Java.util.Collections.frequency() in Java
- Collections max() method in Java with Examples
- Collections.singleton() method in Java with example
- Collections.reverse() in Java with Examples
- Collections.binarySearch() in Java with Examples
- Collections.sort() in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.