How to Create a Map of Maps in Java - Step by Step Examples



How to Create a Map of Maps in Java ?

Answer

To create a map of maps in Java, you can use the HashMap data structure. A map of maps allows you to organize data hierarchically, where each key in the outer map points to another map.



✐ Examples

1 Creating a Map of Maps with Integer Keys and Values

We can create a map of maps in Java by using the HashMap data structure. This example demonstrates creating a map where both the outer and inner maps use integers for keys and values.

For example,

  1. We start by importing the necessary classes: java.util.HashMap and java.util.Map.
  2. We declare and initialize a map named outerMap where each value is another map.
  3. We populate the inner maps with key-value pairs.
  4. We insert the inner maps into the outer map using appropriate keys.
  5. We print the resulting map of maps to the console to verify the structure.

Java Program

import java.util.HashMap;
import java.util.Map;

public class MapOfMaps {
    public static void main(String[] args) {
        // Declare a map of maps
        Map<Integer, Map<Integer, Integer>> outerMap = new HashMap<>();

        // Populate the inner maps
        Map<Integer, Integer> innerMap1 = new HashMap<>();
        innerMap1.put(1, 10);
        innerMap1.put(2, 20);
        innerMap1.put(3, 30);

        Map<Integer, Integer> innerMap2 = new HashMap<>();
        innerMap2.put(4, 40);
        innerMap2.put(5, 50);
        innerMap2.put(6, 60);

        // Insert the inner maps into the outer map
        outerMap.put(1, innerMap1);
        outerMap.put(2, innerMap2);

        // Print the resulting map of maps
        System.out.println("Map of maps with integer keys and values:");
        for (Map.Entry<Integer, Map<Integer, Integer>> outerEntry : outerMap.entrySet()) {
            System.out.println("Outer key: " + outerEntry.getKey());
            for (Map.Entry<Integer, Integer> innerEntry : outerEntry.getValue().entrySet()) {
                System.out.println("  Inner key: " + innerEntry.getKey() + ", value: " + innerEntry.getValue());
            }
        }
    }
}

Output

Map of maps with integer keys and values:
Outer key: 1
  Inner key: 1, value: 10
  Inner key: 2, value: 20
  Inner key: 3, value: 30
Outer key: 2
  Inner key: 4, value: 40
  Inner key: 5, value: 50
  Inner key: 6, value: 60

2 Creating a Map of Maps with Mixed Key and Value Types

We can create a map of maps in Java by using the HashMap data structure. This example demonstrates creating a map where the outer map uses strings for keys and the inner maps use integers for keys and strings for values.

For example,

  1. We start by importing the necessary classes: java.util.HashMap and java.util.Map.
  2. We declare and initialize a map named outerMap where each value is another map with mixed key and value types.
  3. We populate the inner maps with key-value pairs.
  4. We insert the inner maps into the outer map using appropriate keys.
  5. We print the resulting map of maps to the console to verify the structure.

Java Program

import java.util.HashMap;
import java.util.Map;

public class MapOfMaps {
    public static void main(String[] args) {
        // Declare a map of maps
        Map<String, Map<Integer, String>> outerMap = new HashMap<>();

        // Populate the inner maps
        Map<Integer, String> innerMap1 = new HashMap<>();
        innerMap1.put(1, "one");
        innerMap1.put(2, "two");
        innerMap1.put(3, "three");

        Map<Integer, String> innerMap2 = new HashMap<>();
        innerMap2.put(4, "four");
        innerMap2.put(5, "five");
        innerMap2.put(6, "six");

        // Insert the inner maps into the outer map
        outerMap.put("first", innerMap1);
        outerMap.put("second", innerMap2);

        // Print the resulting map of maps
        System.out.println("Map of maps with mixed key and value types:");
        for (Map.Entry<String, Map<Integer, String>> outerEntry : outerMap.entrySet()) {
            System.out.println("Outer key: " + outerEntry.getKey());
            for (Map.Entry<Integer, String> innerEntry : outerEntry.getValue().entrySet()) {
                System.out.println("  Inner key: " + innerEntry.getKey() + ", value: " + innerEntry.getValue());
            }
        }
    }
}

Output

Map of maps with mixed key and value types:
Outer key: first
  Inner key: 1, value: one
  Inner key: 2, value: two
  Inner key: 3, value: three
Outer key: second
  Inner key: 4, value: four
  Inner key: 5, value: five
  Inner key: 6, value: six

3 Creating a Map of Maps with String Keys and Double Values

We can create a map of maps in Java by using the HashMap data structure. This example demonstrates creating a map where both the outer and inner maps use strings for keys and doubles for values.

For example,

  1. We start by importing the necessary classes: java.util.HashMap and java.util.Map.
  2. We declare and initialize a map named outerMap where each value is another map with strings for keys and doubles for values.
  3. We populate the inner maps with key-value pairs.
  4. We insert the inner maps into the outer map using appropriate keys.
  5. We print the resulting map of maps to the console to verify the structure.

Java Program

import java.util.HashMap;
import java.util.Map;

public class MapOfMaps {
    public static void main(String[] args) {
        // Declare a map of maps
        Map<String, Map<String, Double>> outerMap = new HashMap<>();

        // Populate the inner maps
        Map<String, Double> innerMap1 = new HashMap<>();
        innerMap1.put("A", 1.1);
        innerMap1.put("B", 2.2);
        innerMap1.put("C", 3.3);

        Map<String, Double> innerMap2 = new HashMap<>();
        innerMap2.put("D", 4.4);
        innerMap2.put("E", 5.5);
        innerMap2.put("F", 6.6);

        // Insert the inner maps into the outer map
        outerMap.put("group1", innerMap1);
        outerMap.put("group2", innerMap2);

        // Print the resulting map of maps
        System.out.println("Map of maps with string keys and double values:");
        for (Map.Entry<String, Map<String, Double>> outerEntry : outerMap.entrySet()) {
            System.out.println("Outer key: " + outerEntry.getKey());
            for (Map.Entry<String, Double> innerEntry : outerEntry.getValue().entrySet()) {
                System.out.println("  Inner key: " + innerEntry.getKey() + ", value: " + innerEntry.getValue());
            }
        }
    }
}

Output

Map of maps with string keys and double values:
Outer key: group1
  Inner key: A, value: 1.1
  Inner key: B, value: 2.2
  Inner key: C, value: 3.3
Outer key: group2
  Inner key: D, value: 4.4
  Inner key: E, value: 5.5
  Inner key: F, value: 6.6

Summary

In this tutorial, we learned How to Create a Map of Maps in Java language with well detailed examples.




More Java Maps Tutorials

  1. How to create an Empty Map in Java ?
  2. How to create a Map with Initial Key-Value Pairs in Java ?
  3. How to Print a Map in Java ?
  4. How to Add a Key-Value Pair to a Map in Java ?
  5. How to Set a Default Value for a Key in a Map in Java ?
  6. How to Update the Value for a Key in a Map in Java ?
  7. How to Check if a Map is Empty in Java ?
  8. How to Check if a Key Exists in a Map in Java ?
  9. How to Check if a Value Exists in a Map in Java ?
  10. How to Get the Value Associated with a Key in a Map in Java ?
  11. How to Remove a Key-Value Pair from a Map in Java ?
  12. How to Remove Key-Value Pairs from a Map Based on Values in Java ?
  13. How to Clear All Key-Value Pairs from a Map in Java ?
  14. How to Iterate Over Keys in a Map in Java ?
  15. How to Iterate Over Values in a Map in Java ?
  16. How to Iterate Over Entries (Key-Value Pairs) in a Map in Java ?
  17. How to Get the Size (Number of Key-Value Pairs) of a Map in Java ?
  18. How to Convert a Map to an Array of Keys in Java ?
  19. How to Convert a Map to an Array of Values in Java ?
  20. How to Merge Two Maps in Java ?
  21. How to Copy a Map in Java ?
  22. How to Check if Two Maps are Equal in Java ?
  23. How to Sort a Map by Keys in Java ?
  24. How to Sort a Map by Values in Java ?
  25. How to Filter a Map Based on Keys in Java ?
  26. How to Filter a Map Based on Values in Java ?
  27. How to Reduce Values in a Map to a Single Value in Java ?
  28. How to Convert an Array of Key-Value Pairs to a Map in Java ?
  29. How to Convert a Map to a JSON String in Java ?
  30. How to Convert a JSON String to a Map in Java ?
  31. How to Swap Keys and Values in a Map in Java ?
  32. How to Create a Map of Maps in Java ?
  33. How to Iterate Over a Map of Maps in Java ?