Java Nested Map with Examples

Learn to work with Nested HashMap in Java. From creating, removing, and iterating the elements, we will also see the practical use-cases where we can use nested maps.

1. Introduction

HashMap class extends AbstractMap class and implements the Map interface. It holds entry objects i.e. key-value pairs. The keys or the values are of Object types.

A nested HashMap is Map inside a Map. The only difference between a HashMap and a nested HashMap is:

  • For HashMap, the key or the value can be of any type (object).
  • For Nested HashMap, the key can be of any type (object), but the value is another HashMap object only.

Notice the nested HashMap created below; we create a HashMap having String as the key and another ‘HashMapas the value for this map and this is what we call as Nested HashMap.

// Creating Nested HashMap
Map<String, HashMap<Integer, String>> nestedMap = new HashMap<>();

2. When to use a Nested Map?

We use nested maps when we need nested objects i.e. objects inside an object. Let’s take an example of JSON data structure where we have nested objects inside the other objects.

  "Alex": {
    "Hideout": "UAE",
    "Permanent": "Florida",
    "Postal": "Canada"
  "Brian": {
    "Hideout": "India",
    "Permanent": "Alaska",
    "Postal": "Canada"

Nested Maps are hard to scale, making it difficult to read the code (code readability). More importantly, Maps in Java tend to consume a lot of memory. Populating a Map with even more Maps will only aggravate the memory consumption issue. So we have to be careful while using it and it will be used in special scenarios where it is actually required.

How HashMap works?

3. Working with Nested HashMap

To create a nested map, we will first have to create a normal HashMap, just the value of this map will be another HashMap.

3.1. Creating Nested Map using Map.put()

We can use Map interface put() method for adding elements in the outer as well as inner HashMap.

Map<String, Map<String, String>> employeeMap = new HashMap<>();

Map<String, String> addressMap = new HashMap<>();
addressMap.put("Permanent", "Florida");
addressMap.put("Postal", "Canada");

employeeMap.put("Alex", addressMap);

3.2. Creating Nested Map using Streams

We can use stream api to create nested maps using Collectors.toMap() method. In following example, we are creating a ‘Student class having ‘studentId‘ and other fields.

class Student
    private Integer studentId;
    private String studentName;
    private String course;

    // Getters & Setters

We have a list of Student objects, and from this list, we are creating a nested Map having key as 'studentId' and value as another Map with key as studentName and value as course.

// Creating Nested Map
List<Student> studentList = List.of(
    new Student(1, "A", "Course1"),
    new Student(2, "B", "Course2"),
    new Student(3, "C", "Course3"));

Map<Integer, Map<String, String>> studentNestedMap =
        .collect(Collectors.groupingBy(s -> s.getStudentId(),
            Collectors.toMap(Student::getStudentName, Student::getCourse)));

3.3. Adding Element to Nested Map

We can add new elements to an existing nested Map by first retrieving the nested map from the outer map and then using the put() method to add the new elements to the inner map.

Map<String, Map<String, String>> employeeMap = new HashMap<>();

//Adding another address for Alex

employeeMap.get("Alex").put("Hideout", "UAE");

3.4. Removing Element from Nested Map

To remove elements from nested HashMap, invoke remove() similar to the previous example.

Map<String, Map<String, String>> employeeMap = new HashMap<>();

//Hideout has been exposed so removing it


3.5. Iterating over Nested HashMap

We can iterate over a nested map just like we will iterate over a normal HashMap.

Map<String, Map<String, String>> employeeMap = new HashMap<>();

for (Map.Entry<String, Map<String, String>> empMap : employeeMap.entrySet()) {
  Map<String, String> addMap = empMap.getValue();

  // Iterate InnerMap
  for (Map.Entry<String, String> addressSet : addMap.entrySet()) {
    System.out.println(addressSet.getKey() + " :: " + addressSet.getValue());

4. Conclusion

We have learned the Nested Maps and how to create them and add, remove and iterate over the elements. We have also seen where we can use the nested maps.

Happy Learning !!

Sourcecode on Github

Leave a Reply

Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial