Iterate through nested hashmap
for (Map.Entry<String, HashMap<String, Student>> letterEntry : students.entrySet()) {
String letter = letterEntry.getKey();
// ...
for (Map.Entry<String, Student> nameEntry : letterEntry.getValue().entrySet()) {
String name = nameEntry.getKey();
Student student = nameEntry.getValue();
// ...
}
}
...and the var
keyword in Java 10 can remove the generics verbosity:
for (var letterEntry : students.entrySet()) {
String letter = letterEntry.getKey();
// ...
for (var nameEntry : letterEntry.getValue().entrySet()) {
String name = nameEntry.getKey();
Student student = nameEntry.getValue();
// ...
}
}
Java 8 lambdas and Map.forEach
make bkail's answer more concise:
outerMap.forEach((letter, nestedMap) -> {
//...
nestedMap.forEach((name, student) -> {
//...
});
//...
});