我有以下 HashMap:
private HashMap<HashMap<Integer,String>,ArrayList<String>> unique_schemas = new HashMap<HashMap<Integer,String>,ArrayList<String>>();
我在向其中添加条目或打印其内容时没有任何问题,但我不确定如何检查现有条目。
我已经尝试过:
//create temp HashMap to check against
HashMap<Integer,String> mapkey = new HashMap<Integer,String>();
//populate it with the values to check for
mapkey.put(parentId,text);
if (unique_schemas.containsKey(mapkey.get(0))) {
//do whatever when the entry exists
}
这不起作用,我知道某个地方出了问题,但我不知所措。谁能解释一下我的问题吗?
最佳答案
您应该将支票更改为:
//create temp HashMap to check against
HashMap<Integer,String> mapkey = new HashMap<Integer,String>();
//populate it with the values to check for
mapkey.put(parentId,text);
if (unique_schemas.containsKey(mapkey)) {
//do whatever when the entry exists
}
如果您确定映射中只有一对,您也可以只向映射分配 1 个元素:
private HashMap<HashMap<Integer,String>,ArrayList<String>> unique_schemas = new HashMap<HashMap<Integer,String>,ArrayList<String>>(1);
关于java - 检查 key 是否存在于 hashmap 中,其中 key 是嵌套的 hashmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25484547/