条款9:“当您覆盖等于时,始终覆盖hashCode()". [英] Item-9: "Always override hashCode() when you override equals"

查看:61
本文介绍了条款9:“当您覆盖等于时,始终覆盖hashCode()".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于以下提到的3个合同:

With respect to 3 contracts mentioned below:

1)在执行应用程序期间,每次在同一对象上多次调用 hashCode()时, hashCode 方法必须始终返回相同的整数,前提是没有修改在对象的相等比较中使用的信息.从应用程序的一次执行到同一应用程序的另一次执行,该整数不必保持一致.

1) Whenever hashCode() is invoked on the same object more than once during an execution of an 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.

从这句话中,我了解到,在应用程序的一次执行中,如果在同一对象上使用 hashCode()一次或多次,则它应该返回相同的值.

From this statement, i understand that, In a single execution of an application, if hashCode() is used one or more times on same object it should return same value.

2)如果根据 equals(Object)方法两个对象相等,则在两个对象中的每个对象上调用 hashCode()方法必须产生相同的结果整数结果.

2) 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.

从这句话中,我了解到,要在子类中执行相等操作(在广泛范围内),至少要有四个不同的相等度.

From this statement, i understand that, to perform the equality operation(in broad scope) in your subclass, There are at least four different degrees of equality.

(a)引用相等(==),比较两个引用类型对象的内部地址.

(a) Reference equality(==), comparing the internal address of two reference type objects.

(b)浅结构相等:如果两个对象的所有字段均为==,则它们是相等".{例如,两个 SingleLinkedList ,它们的大小"字段相等,并且其头"字段指向相同的 SListNode .}

(b) Shallow structural equality: two objects are "equals" if all their fields are ==. { For example, two SingleLinkedList whose "size" fields are equal and whose "head" field point to the same SListNode.}

(c)深层结构等式:如果两个对象的所有字段均为等式",则它们是等式".{例如,代表相同项目"序列的两个 SingleLinkedList (尽管 SListNodes 可能不同).}

(c) Deep structural equality: two objects are "equals" if all their fields are "equals". {For example, two SingleLinkedList that represent the same sequence of "items" (though the SListNodes may be different).}

(d)逻辑相等.{两个例子:(a)即使两个集合"对象包含相同的元素,即使它们以不同顺序存储这些元素,它们也是等于"的.(b)分数1/3和2/6是相等的",即使它们的分子和分母都不同.}

(d) Logical equality. {Two examples: (a) Two "Set" objects are "equals" if they contain the same elements, even if the underlying lists store the elements in different orders. (b) The Fractions 1/3 and 2/6 are "equals", even though their numerators and denominators are all different.}

基于以上四个等式,第二个合同仅适用:if(Say) equals()方法基于两个对象之间的逻辑相等返回真值,然后 hashCode()方法必须在计算每个新对象的整数之前也要考虑逻辑计算的平等性,而不是考虑新对象的内部地址.

Based on above four categories of equality, second contract will hold good only: if(Say) equals() method returns truth value based on logical_equality between two objects then hashCode() method must also consider logical_equality amidst computation before generating the integer for every new object instead of considering internal address of a new object.

但是我在理解这份第三份合同时遇到了问题.

But i have a problem in understanding this third contract.

3)不需要,如果根据 equals(Object)方法两个对象不相等,则分别对这两个对象分别调用 hashCode()方法对象必须产生不同的整数结果.但是,程序员应该意识到,为不相等的对象生成不同的整数结果可能会提高哈希表的性能.

3) IT IS NOT REQUIRED that if two objects are unequal according to the equals(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 hash tables.

在第二份合同中,正如我们所说的,应该相应地实施 hashCode()方法[例如:在生成整数之前考虑逻辑平等],这是不正确的可以说,如果根据 equals(Object)两个对象不相等,那么 hashCode()方法可能会产生与第三合同所述相同的整数结果?根据第二份合同中的参数, hashCode() 必须产生不同的整数结果.刚刚在 hashCode()中编写 return 42 的人违反了第二份合同!

In second contract, As we are saying that hashCode() method should be accordingly[for ex: considering logical_equality before generating integer] implemented, I feel, It is not true to say that, if two objects are unequal according to equals(Object) then hashCode() method may produce same integer results as mentioned in third contract? As per the argument in second contract, hashCode() must produce distinct integer results. One just writing return 42 in hashCode() is breaking second contract!

请帮助我理解这一点!

推荐答案

hashCode()总是不可能为不相等的对象返回不同的值.例如,有2 ^ 64个不同的 Long 值,但只有2 ^ 32个可能的 int 值.因此, Long hashCode()方法必须重复一些.在这种情况下,您必须尽力确保您的 hashCode()方法尽可能均匀地分配值,并且不太可能对您在实践中最可能使用的实例产生重复.

It would be impossible for hashCode() to always return different values for unequal objects. For example, there are 2^64 different Long values, but only 2^32 possible int values. Therefore the hashCode() method for Long has to have some repeats. In situations like this you have to try hard to ensure that your hashCode() method distributes values as evenly as possible, and is unlikely to produce repeats for the instances you are most likely to use in practice.

第二个条件只是说两个 equal()实例必须返回相同的 hashCode()值,因此该程序必须输出true:

The second condition just says that two equal() instances must return the same hashCode() value, so this program must print true:

Long a = Long.MIN_VALUE;
Long b = Long.MIN_VALUE;
System.out.println(a.hashCode() == b.hashCode()); // a.equals(b), so must print true.

但是此程序也会显示true:

However this program also prints true:

Long c = 0L;
Long d = 4294967297L;
System.out.println(c.hashCode() == d.hashCode()); // prints true even though !c.equals(d)

这篇关于条款9:“当您覆盖等于时,始终覆盖hashCode()".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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