Four Methods for Iterating Nested Map Collections

public Student() {
    super();
}

public Student(String name, int grade) {
    super();
    this.name = name;
    this.grade = grade;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getGrade() {
    return grade;
}

public void setGrade(int grade) {
    this.grade = grade;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + grade;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Student other = (Student) obj;
    if (grade != other.grade)
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

@Override
public String toString() {
    return "Student [name=" + name + ", grade=" + grade + "]";
}

}


</div>Important considerations: ① Ensure proper import statements ② Correct usage of entrySet and keySet methods ③ When using iterators, first create the collection; enhanced for loops work normally (since Maps don't support indexed access, standard for loops cannot be used) <div>```
package com.oracle.example;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class ExampleNestedMap {

    public static void main(String[] args) {
        // Outer map
        HashMap<String, HashMap<Student, String>> school = new HashMap<String, HashMap<Student, String>>();
        // Inner maps
        HashMap<Student, String> class1018 = new HashMap<Student, String>();
        HashMap<Student, String> class1227 = new HashMap<Student, String>();
        
        // Populate inner maps
        class1018.put(new Student("Alice", 90), "Mathematics");
        class1018.put(new Student("Bob", 85), "Physics");
        class1227.put(new Student("Charlie", 92), "Chemistry");
        class1227.put(new Student("David", 88), "Biology");
        
        // Populate outer map
        school.put("class1018", class1018);
        school.put("class1227", class1227);
        
        // Method 1: Using entrySet with iterator
        Set<Map.Entry<String, HashMap<Student, String>>> bigEntries = school.entrySet();
        Iterator<Map.Entry<String, HashMap<Student, String>>> bigIterator = bigEntries.iterator();
        while (bigIterator.hasNext()) {
            Map.Entry<String, HashMap<Student, String>> bigEntry = bigIterator.next();
            String className = bigEntry.getKey();
            HashMap<Student, String> innerMap = bigEntry.getValue();
            Set<Entry<Student, String>> innerEntries = innerMap.entrySet();
            Iterator<Entry<Student, String>> innerIterator = innerEntries.iterator();
            while (innerIterator.hasNext()) {
                Entry<Student, String> innerEntry = innerIterator.next();
                System.out.println(className + "..." + innerEntry.getKey() + "..." + innerEntry.getValue());
            }
        }
        
        System.out.println("=========================");
        
        // Method 2: Enhanced for loop with entrySet
        Set<Entry<String, HashMap<Student, String>>> entries = school.entrySet();
        for (Entry<String, HashMap<Student, String>> entry : entries) {
            String className = entry.getKey();
            HashMap<Student, String> innerMap = entry.getValue();
            Set<Entry<Student, String>> innerEntries = innerMap.entrySet();
            for (Entry<Student, String> innerEntry : innerEntries) {
                System.out.println(className + "..." + innerEntry.getKey() + "..." + innerEntry.getValue());
            }
        }
        
        System.out.println("3. Enhanced for with keySet=========================");
        
        // Method 3: Enhanced for with keySet
        Set<String> classKeys = school.keySet();
        for (String className : classKeys) {
            HashMap<Student, String> innerMap = school.get(className);
            Set<Student> studentSet = innerMap.keySet();
            for (Student student : studentSet) {
                System.out.println(className + "..." + student + "..." + innerMap.get(student));
            }
        }
        
        System.out.println("4. Iterator with keySet=========================");
        
        // Method 4: Iterator with keySet
        Set<String> classKeys2 = school.keySet();
        Iterator<String> classIterator = classKeys2.iterator();
        while (classIterator.hasNext()) {
            String className = classIterator.next();
            HashMap<Student, String> innerMap = school.get(className);
            Set<Student> studentSet = innerMap.keySet();
            Iterator<Student> studentIterator = studentSet.iterator();
            while (studentIterator.hasNext()) {
                Student student = studentIterator.next();
                System.out.println(className + "..." + student + "..." + innerMap.get(student));
            }
        }
    }
}

Tags: java Map Collection NestedCollections Iteration

Posted on Wed, 12 Aug 2026 16:38:14 +0000 by callmubashar