您从嵌套的 Map 中获取值的方式与从未嵌套的 Map 中获取值的方式相同,您只需应用相同的过程两次:
//Java7 Diamond Notation
Map<ArrayList, Map<String, Integer>> nestedMap = new HashMap<>();
//get nested map
Map<String, Integer> innerMap = nestedMap.get(some_key_value_string);
//now get the Integer value from the innerMap
Integer innerMapValue = innerMap.get(some_key_value_string);
此外,如果您正在寻找特定的键,您可以像这样遍历地图:
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println("Key: " + pairs.getKey() + " Val: " + pairs.getValue()));
it.remove(); // avoids a ConcurrentModificationException
}
这将遍历单个映射的所有键和值。
希望这可以帮助。