Multidimensional Collections in Java



In this article, we will learn about multidimensional collections in Java. These collections offer dynamic resizing, flexible data storage, and better memory management compared to traditional arrays.

What are Multidimensional collections?

Multidimensional collections are also known as Nested collections. It is a group of objects wherein every group has any number of objects that can be created dynamically. They can be stored in any position as well. In the case of arrays, the user would be bound to a specific number of rows and columns, hence multidimensional structure helps create and add elements dynamically.

Different Approaches

The following are the two different approaches to creating a multidimensional collection in Java ?

Using Nested ArrayList

Java's ArrayList allows dynamic resizing, making it ideal for multidimensional collections where the size of inner lists may vary.

Syntax of multidimensional ArrayList in Java

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

Following are the steps to create a multidimensional collection using nested ArrayList ?

  • ArrayList<ArrayList<Integer>> A list of lists to create a flexible 2D structure.
  • Dynamic Insertions: Elements and sublists are added dynamically using add() method.
  • Pre-populated Lists: Used Arrays.asList() to initialize lists with predefined values.
ArrayList<ArrayList<Integer> > x = new ArrayList<ArrayList<Integer> >();

Example

Below is an example of multidimensional collections using nested ArrayList ?

import java.util.*;
public class Demo {
   static List multi_dimensional() {
      ArrayList<ArrayList<Integer> > x = new ArrayList<ArrayList<Integer> >();
      x.add(new ArrayList<Integer>());
      x.get(0).add(0, 45);
      x.add(new ArrayList<Integer>(Arrays.asList(56, 67, 89)));
      x.get(1).add(0, 67);
      x.get(1).add(4, 456);
      x.add(2, new ArrayList<>(Arrays.asList(23, 32)));
      x.add(new ArrayList<Integer>(Arrays.asList(83, 64, 77)));
      x.add(new ArrayList<>(Arrays.asList(8)));
      return x;
   }
   public static void main(String args[]) {
      System.out.println("The multidimensional arraylist is :");
      System.out.println(multi_dimensional());
   }
}

Output

The multidimensional arraylist is :
[[45], [67, 56, 67, 89, 456], [23, 32], [83, 64, 77], [8]]

Using HashMap with Lists

Another improved way to store multidimensional collections are by using a HashMap<Integer, List<Integer>>. This provides faster lookups using keys and ensures random access without index-related errors.

Following are the steps to create a multidimensional collection using HashMap with lists ?

  • HashMap<Integer, List<Integer>>: Uses unique row indices as keys.
  • Flexible List Lengths: Each key can store lists of different sizes.
  • Fast Retrieval: Direct access via keys instead of index-based searching.
Map<Integer, List<Integer>> multiMap = new HashMap<>();

Example

Below is an example of multidimensional collections using HashMap with lists ?

import java.util.*;

public class MultiDimensionalHashMap {
   static Map<Integer, List<Integer>> createMultiDimensionalMap() {
      Map<Integer, List<Integer>> multiMap = new HashMap<>();

      multiMap.put(0, new ArrayList<>(Arrays.asList(10, 20, 30))); // Row 0
      multiMap.put(1, new ArrayList<>(Arrays.asList(40, 50))); // Row 1
      multiMap.put(2, new ArrayList<>(Arrays.asList(60, 70, 80, 90))); // Row 2
      multiMap.put(3, Arrays.asList(100, 110)); // Row 3 (Immutable List)

      return multiMap;
   }

   public static void main(String args[]) {
      System.out.println("The multidimensional HashMap is:");
      System.out.println(createMultiDimensionalMap());
   }
}

Output

The multidimensional HashMap is:
{0=[10, 20, 30], 1=[40, 50], 2=[60, 70, 80, 90], 3=[100, 110]}

Conclusion

Multidimensional collections in Java provide a flexible way to handle complex data structures. Use ArrayList when working with dynamic 2D data with fixed ordering. Use HashMap for efficient lookups and key-based grouping.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-03-19T18:52:59+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements