Iterate through an attached hash file

How can I iterate through a nested HashMap?

HashMap installed as follows:

 HashMap<String, HashMap<String, Student>> 

Where Student is an object containing the name variable. If, for example, my HashMap looked like this (the following code is not mine, it's just to simulate the contents of the contents of the hash map)

  hm => HashMap<'S', Hashmap<'Sam', SamStudent>> HashMap<'S', Hashmap<'Seb', SebStudent>> HashMap<'T', Hashmap<'Thomas', ThomasStudent>> 

How can I repeat all keys with a single letter, then each full key, and then pull out the student’s name?

+7
java hashmap
source share
2 answers
 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(); // ... } } 
+11
source share

Java 8 lambdas and Map.forEach make bkail answer more concise:

 outerMap.forEach((letter, nestedMap) -> { //... nestedMap.forEach((name, student) -> { //... }); //... }); 
+9
source share

All Articles