How can i fetch all data present in Map<String,Map<String,String>> data structure?

StackOverflow https://stackoverflow.com/questions/21852961

  •  13-10-2022
  •  | 
  •  

Question

i want to retrieve all data that are present in my data structure, which is of type Map of Maps.The data structure is mentioned below.

public static Map<String, Map<String, String>> hourlyMap = new HashMap<String, Map<String, String>>();

i need all the data that are stored in the map irrespective of key.

Was it helpful?

Solution

This may help you

Map<String,Map<String,String>> hourlyMap = new HashMap<String,Map<String,String>>();
    for(Map<String,String> i:hourlyMap.values()){
        // now i is a Map<String,String>
        for(String str:i.values()){
            // now str is a value of map i
            System.out.println(str);
        }
    }

OTHER TIPS

Try:

Set<String> allData = new HashSet<>();     // will contain all the values
for(Map<String, String> map : hourlyMap.values()) {
    allData.addAll(map.values());
}
    for (String outerKey: hourlyMap.keySet()) {
        // outerKey holds the Key of the outer map
        // the value will be the inner map - hourlyMap.get(outerKey)

        System.out.println("Outer key: " + outerKey);
        for (String innerKey: hourlyMap.get(outerKey).keySet()) {

            // innerKey holds the Key of the inner map
            System.out.println("Inner key: " + innerKey);

            System.out.println("Inner value:" + hourlyMap.get(outerKey).get(innerKey));
        }

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top