我正在尝试从HashMap中的HashMap获取某个值:
outermap的类型为HashMap<String,HashMap<String,String>>
,其中包含以下内容:
{
"9000": {
"property": "Art",
"general": "no",
"finished": "yes"
},
"10503": {
"property": "Art",
"general": "no",
"finished": "no"
},
"16792": {
"property": "Art",
"general": "no",
"finished": "yes"
}
}
我希望能够确定内部HashMap的密钥“finished”是否具有值“yes”
您可以帮助我提取这些信息吗?无法解决问题......
这可能是因为我对如何迭代多个哈希图有点困惑,我应该为内图填充另一个哈希图吗?然后迭代那个?
很抱歉,如果这没有意义......我可以尝试澄清一下。任何帮助将不胜感激。
答案 0 :(得分:1)
尝试outerMap.get("9000").get("finished")
有几种迭代地图的方法,最常见的方法是获取其keySet
for (String outerMapKey : outerMap.keySet()) {
System.out.println(outerMap.get(outerMapKey).get("finished"));
}
答案 1 :(得分:0)
Map <String, HashMap <String, String>> outerMap;
outerMap.get("16792").get("finished");
答案 2 :(得分:0)
首先,您可以将字符串转换为Map&gt;使用jsonutils。 其次,过程。
Map<String,Map<String,String>> map = new ObjectMapper().readValue(str,Map.class);
for (String key : map.keySet()){
Map<String,String> innerMap = map.get(key);
if(StringUtils.equals(innerMap.get("finished"),"true")){
System.out.println("finished is true ,key is " + key);
}
}
答案 3 :(得分:0)
// create dummy data set
Map<String, Map<String, String>> data = new HashMap<>();
Map<String, String> item = new HashMap<>();
item.put("property", "Art");
item.put("general", "no");
item.put("finished", "yes");
data.put("9000", item);
item = new HashMap<>();
item.put("property", "Art");
item.put("general", "no");
item.put("finished", "no");
data.put("10503", item);
item = new HashMap<>();
item.put("property", "Art");
item.put("general", "no");
item.put("finished", "yes");
data.put("16792", item);
// end dummy data set
// iterate to find the desired item
for (String key : data.keySet()) {
Map<String, String> items = data.get(key);
if (items.containsKey("finished") && "yes".equals(items.get("finished"))) {
System.out.println(key);
}
}