2014-02-18 89 views
0

我想要检索我的数据结构中存在的所有数据,这些数据结构是Map of Map的类型。数据结构如下所述。如何获取Map <String,Map <String,String >>数据结构中的所有数据?

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

我需要存储在地图中的所有数据,而不考虑密钥。

+1

你看地图的文件? –

+0

Map 我试过但无法实现上面的数据结构。 – Satya

+0

keySet()会给你外键映射..迭代的值和每个值,获取keySet和值.. – TheLostMind

回答

3

这可能会帮助你

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); 
     } 
    } 
+0

它的工作..如果我想显示外部地图中的每个键与内部地图中的相关键值对的值。那么应该是什么逻辑。 – Satya

3

尝试:

Set<String> allData = new HashSet<>();  // will contain all the values 
for(Map<String, String> map : hourlyMap.values()) { 
    allData.addAll(map.values()); 
} 
+0

+1,因为它是最简洁的变体。但是,它应该是循环中的'.values()'。 – qqilihq

1
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)); 
     } 

    } 
相关问题