为什么我的哈希图不起作用?我的对象具有以下属性:hashCode()相等意味着equals(...)相等 [英] Why doesn't my hashmap work? My object has the property that hashCode() equality implies equals(...) equality

查看:139
本文介绍了为什么我的哈希图不起作用?我的对象具有以下属性:hashCode()相等意味着equals(...)相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java HashMap为什么不起作用?我的对象具有.equals相等性意味着hashCode相等性的属性.

Why doesn't my Java HashMap work? My object has the property that .equals equality implies hashCode equality.

您可以假定我将对象添加到HashMap之后正在修改对象的字段.

You can assume that I'm modifying a field of the object after I add the object to the HashMap.

推荐答案

您可以假定我将对象添加到HashMap之后正在修改对象的字段.

那是有原因的.

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.

未指定"的意思是可能不起作用",因此当它不起作用时,您应该不会感到惊讶.

"Not specified" means "may not to work", so you should not be surprised when it doesn't work.

这篇关于为什么我的哈希图不起作用?我的对象具有以下属性:hashCode()相等意味着equals(...)相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆