更新 Java HashMap 键
问题说明
我只是想知道,如果 HashMap
的键是可变的会发生什么,下面的测试程序证明了这一点,我无法理解 equals 和 hashCode
方法何时返回true 和相同的值,为什么 hashmap.containsKey
返回 false
.
I was just wondering, what would happen if key of a HashMap
is mutable, test program below demonstrate that and I am unable to understand when both equals and hashCode
methods returns true and same value, why does hashmap.containsKey
return false
.
public class MutableKeyHashMap {
public static void main(String []a){
HashMap<Mutable, String> map = new HashMap<Mutable, String>();
Mutable m1 = new Mutable(5);
map.put(m1, "m1");
Mutable m2 = new Mutable(5);
System.out.println(map.containsKey(m2));
m2.setA(6);
m1.setA(6);
Mutable m3 = map.keySet().iterator().next();
System.out.println(map.containsKey(m2) " " m3.hashCode() " " m2.hashCode() " " m3.equals(m2));
}
}
class Mutable {
int a;
public Mutable(int a) {
this.a = a;
}
@Override
public boolean equals(Object obj) {
Mutable m = (Mutable) obj;
return m.a == this.a ? true : false;
}
@Override
public int hashCode(){
return a;
}
public void setA(int a) {
this.a = a;
}
public int getA() {
return a;
}
}
这是输出:
真假 6 6 真
正确答案
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.
基本上,不要在 Map 中使用可变对象作为键,否则会被烧毁
Basically, don't use mutable objects as keys in a Map, you're going to get burnt
推断,因为文档可能看起来不清楚,我相信这里的相关点是以影响 equals 的方式更改",并且您似乎假设每次调用 contains 时都会调用 equals(Object).文档没有这么说,措辞暗示它们可能被允许缓存计算.
To extrapolate, because the docs may not appear clear, I believe the pertinent point here is `changed in a manner that affects equals', and you seem to be assuming that equals(Object) is called each time contains is invoked. The docs don't say that, the wording implies they may be allowed to cache computations.
查看source,似乎是因为您的hashCode 返回一个不同的值(以前是 5,现在是 6),可能是根据实现细节在不同的存储桶中查找它.
Looking at the source, it seems that because your hashCode returns a different value (was 5, now 6), it's possible that it's being looked up in a different bucket based on implementation details.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangicbbb
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20