Relationship between hashCode and equals method in Java
我在很多地方读过,在Java中重写EDCOX1 0的方法时,也应该重写EDCOX1×1的方法,否则它是"违反合同"。
但到目前为止,如果我只重写equals方法,而不重写hashcode方法,我还没有遇到任何问题。
合同是什么?当我违反合同时,为什么我没有面临任何问题?在哪种情况下,如果我没有重写hashcode方法,我将面临一个问题?
您将遇到的问题是集合,其中元素的唯一性是根据
顾名思义,它依赖于哈希表,哈希桶是对象的
如果有两个对象是
合同中最重要的部分是:对象是
这一切都记录在JavaDocfor
根据文档,hashcode的默认实现将返回每个对象不同的整数。
As much as is reasonably practical, the hashCode method defined by class Object does
return distinct integers for distinct objects. (This is typically implemented by
converting the internal address of the object into an integer, but this implementation
technique is not required by the JavaTM programming language.)
号
但是,有时您希望哈希代码对于具有相同含义的不同对象是相同的。例如
1 2 3 | Student s1 = new Student("John", 18); Student s2 = new Student("John", 18); s1.hashCode() != s2.hashCode(); // With the default implementation of hashCode |
如果在收集框架中使用哈希数据结构(如hashtable、hashset),就会出现这种问题。尤其是对于hashset这样的集合,您将最终拥有重复的元素并违反集合约定。
是的,它应该被覆盖。如果您认为需要覆盖
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
号
见江户记1〔12〕的雅瓦多。
在
If two objects are equal according to the
equals(Object) method,
then calling thehashCode method on each of the two objects must
produce the same integer result.
号
(我强调)。
如果您只覆盖
在
Note that it is generally necessary to override the
hashCode method
whenever this method is overridden, so as to maintain the general
contract for thehashCode method, which states that equal objects must
have equal hash codes.
号
合同规定,如果
约定是:如果两个对象相等,那么它们应该具有相同的哈希代码;如果两个对象不相等,那么它们可能具有相同的哈希代码,也可能不具有相同的哈希代码。
尝试使用对象作为hashmap中的键(在JoachimSauer的评论后编辑),你将开始面临麻烦。合同是指导方针,不是强加给你的东西。
看看
当不重写