- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个用于存储对象的 HashMap:
private Map<T, U> fields = Collections.synchronizedMap(new HashMap<T, U>());
但是,当尝试检查 key 是否存在时,containsKey
方法返回 false
。
实现了equals
和hashCode
方法,但是没有找到key。
调试一段代码时:
return fields.containsKey(bean) && fields.get(bean).isChecked();
我有:
bean.hashCode() = 1979946475
fields.keySet().iterator().next().hashCode() = 1979946475
bean.equals(fields.keySet().iterator().next())= true
fields.keySet().iterator().next().equals(bean) = true
但是
fields.containsKey(bean) = false
什么会导致这种奇怪的行为?
public class Address extends DtoImpl<Long, Long> implements Serializable{
<fields>
<getters and setters>
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + StringUtils.trimToEmpty(street).hashCode();
result = prime * result + StringUtils.trimToEmpty(town).hashCode();
result = prime * result + StringUtils.trimToEmpty(code).hashCode();
result = prime * result + ((country == null) ? 0 : country.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Address other = (Address) obj;
if (!StringUtils.trimToEmpty(street).equals(StringUtils.trimToEmpty(other.getStreet())))
return false;
if (!StringUtils.trimToEmpty(town).equals(StringUtils.trimToEmpty(other.getTown())))
return false;
if (!StringUtils.trimToEmpty(code).equals(StringUtils.trimToEmpty(other.getCode())))
return false;
if (country == null) {
if (other.country != null)
return false;
} else if (!country.equals(other.country))
return false;
return true;
}
}
最佳答案
key 插入 map 后不得修改。
编辑:我在 Map 中找到了 javadoc 的摘录。 :
Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map.
一个简单的包装类的例子:
public static class MyWrapper {
private int i;
public MyWrapper(int i) {
this.i = i;
}
public void setI(int i) {
this.i = i;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return i == ((MyWrapper) o).i;
}
@Override
public int hashCode() {
return i;
}
}
和测试:
public static void main(String[] args) throws Exception {
Map<MyWrapper, String> map = new HashMap<MyWrapper, String>();
MyWrapper wrapper = new MyWrapper(1);
map.put(wrapper, "hello");
System.out.println(map.containsKey(wrapper));
wrapper.setI(2);
System.out.println(map.containsKey(wrapper));
}
输出:
true
false
注意:如果你不覆盖 hashcode() 那么你只会得到 true
关于Java HashMap containsKey 为现有对象返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21600344/
java.util.Map.containsKey(Object key)文件说: @throws ClassCastException if the key is of an inappropria
我正在为大学类(class)实现一个 Web 项目,该项目使用 struts2,当我运行该项目时,它会在 containsKey() 中阻塞。它不打印任何字符串。 这是我遇到问题的一些代码: pack
我的字典的 ContainsKey() 方法不起作用 - 我如何“覆盖 GetHashCode()”以使其起作用? 最佳答案 您必须同时覆盖 GetHashCode()和 Equals()来表示等价。
我正在尝试将超链接存储为 map 中的键,我正在使用 multimap private Map> record=new HashMap>(); 我正在使用以下方法检查超链接是否已经存在于 map 中
好的,我需要一点帮助。我有一本通用词典“cashdata”。这个字典的键是对象(查询对象,我定义的一个类)。查询对象有“Terms”字段,它是一个字符串列表,和一个“Operator”字段,它是一个枚
我在用 gradle 构建我的 Android 项目时遇到了这个错误。它大约每周发生一次。 FAILURE: Build failed with an exception. * What went w
尝试实现陷入奇怪问题的简单任务: class User{ String login; String pwrd; User(String lg,String pw){
使用此代码时出现崩溃: if (!savedInstanceState.containsKey("mybool")) { // my code } 我的onCreate(): @Overri
我正在用 java 编写 HashMap 的实现,但在 containsKey 方法中查找正确的存储桶时遇到了一些问题 public class MyHashMap { LinkedList[] ta
构造函数接受两个数组,我想将它们放入我的 HashMap 中,但如果映射已经包含其中一个数组中的单词,那么我想抛出异常。除非重复项是数组项中的最后一项,否则这种方法有效。为什么会发生这种情况? pri
程序将根据If语句打印不正确的键和值。有人能解释一下为什么吗? 例如键 = 汤姆叔叔 + 值 = 02086542222 键 = Harry + 值 = 020826262 查询=汤姆叔叔 返回 =
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在尝试将位置列表中的所有唯一坐标添加到单个 HashMap 中,该 HashMap 将坐标作为键,将计数作为值。关键是由“$”符号连接的经度和纬度。 //String = coordinates
This question already has answers here: Java HashMap containsKey returns false for existing object (
这个问题已经有答案了: What is the Java ?: operator called and what does it do? (17 个回答) Can Java's ternary/con
我基本上是在尝试将一些组织的 ID 和名称缓存在字典中。我不断收到错误消息:Item with Same Key has already been added。这对我来说很奇怪,因为第一个 if bl
我正在为一个使用家庭概念的应用程序创建一个插件。每个 Family 都属于一个 FamilyCategory,每个 Family 都包含 FamilySymbols。像这样的漂亮树结构: 家庭类别(门
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 7 年前。 我对 Java 很陌生。尝试访问
这是我的代码: import java.util.HashMap; class MyString { String string; MyString(String string) {
请解释字典的'getAt'方法失败的原因 List infoKeys = new List(infoDict.Keys); if (infoKeys.Contains(TorrentFileKeyWo
我是一名优秀的程序员,十分优秀!