java - 检查 key 是否存在于 hashmap 中,其中 key 是嵌套的 hashmap

标签 java hashmap

我有以下 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/

相关文章:

java - 这个散列探测方法是如何二次的?

java - 如何在 Java 中遍历 Map?

Java 将输入追加到文件

java - 如何失去textField onClick的焦点

java - Spring 启动:run works but java-jar doesn't

java - 如何在 Java 中获得两个具有容差的 HashMap 的交集?

java - 文件引用,正确的语法是什么

java - org.springframework.beans.factory.BeanCreationException : Error creating bean with name 'triangle' defined in class path resource

java - java.util.Property 与 hashmap 的性能方面

java - 在Java中获取 HashMap 中的值