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?
It will be
entry.getValue().entrySet()sothen you can use
It is worth mentioning that, this can also be done Using java 8 Streams and lambda expressions
mapofmaps.forEach((K,V): This expects a lambda expressions which takes two inputs i.e Key (String) and Value (HashMap)V.forEach((X,Y)->{: As this is applied on inner (V: fetched through previous foreach) hashmap so both Key and Value will be stringsReference for further reading :