Betafish Betafish - 1 month ago 17
Java Question

Printing HashMap of HashMaps

I have a method which returns out hashmap of hashmaps

HashMap<String, HashMap<String, String>> mapofmaps = abcd(<String>, <Integer>);


I am trying to print the the outer hashmap using the following code

for (Entry<String, HashMap<String, String>> entry : mapofmaps.entrySet()) {
String key = entry.getKey();
System.out.println(key);

HashMap<String, String> value = entry.getValue();
System.out.println(key + "\t" + value);
}


I would like to iterate through the inner map. What would be the entryset variable there (??? in the code).

for (Entry<String, HashMap<String, String>> entry : mapofmaps.entrySet()) {
String key = entry.getKey();
System.out.println(key);
for(Entry<String, HashMap<String, String>> entry : ????.entrySet()){
HashMap<String, String> value = entry.getValue();
System.out.println(key + "\t" + value);
}}


Is my logic for printing the hashmaps correct? or is there a better way to do that?

Answer

it will be entry.getValue().entrySet() so

 for(Entry<String, String> innerEntry : entry.getValue().entrySet()){

then you can use

    String key    = innerEntry.getKey();
    String value  = innerEntry.getValue();