获取值从HashMap中嵌套到另一个地图 [英] Getting value from Nested HashMap into another Map
问题描述
您好,我想存储嵌套地图,看起来像这样的键和值:
Hi, I wanted to store key and value of a nested Map that looks like this:
Map<ArrayList <String>, Map<String, Integer>> NestedMap = new HashMap<ArrayList<String>, Map<String, Integer>();
到另一个变量让说getKeyFromInsideMap和getValueFromInsideMap。因此,这将是地图内字符串和整数的那有兴趣访问值。我怎么这在code?
搜索结果我试了几个例子在这里的论坛,但我不知道语法会是什么样子。能否请您提供一些code这一点。谢谢!
into another variable let say getKeyFromInsideMap and getValueFromInsideMap. So it would be the values of the inside Map String and Integer that am interested in accessing. How do I this in a code?
I tried several examples here in the forum but I don't know how the syntax would look like. Could you please provide some code for this. Thank you!
推荐答案
您得到当你从一个嵌套的地图让他们嵌套通过地图方式相同的价值观,你只需要申请两次相同的过程:
You get the values from the nested by Map the same way as you get them from an unnested Map, you just have to apply the same process twice:
//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);
此外,如果你正在寻找一个特定的键,你可以在地图像这样在迭代:
Also if you are looking for a specific key, you can iterate over the map like so:
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
}
这会遍历所有的按键和价值的单的地图。
this will iterate over all of the keys and value of a single map.
希望这有助于。
这篇关于获取值从HashMap中嵌套到另一个地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!