简体   繁体   English

等于和哈希码的不同字段

[英]Different fields for equals and hashcode

I agree with the statement from this post What issues should be considered when overriding equals and hashCode in Java? 我同意本文中的说法, 在Java中重写equals和hashCode时应考虑哪些问题?

Use the same set of fields that you use to compute equals() to compute hashCode(). 使用用于计算equals()和计算hashCode()的同一组字段。

But i've some doubts : 但是我有一些疑问:

  • Is this absolutely necessary to have same fields ? 具有相同的字段是否绝对必要?
  • If yes, what if I don't use same field ? 如果是,如果我不使用同一字段怎么办?
  • Will it affect HashMap performance or HashMap Accuracy ? 它会影响HashMap的性能还是HashMap的准确性?

Is this absolutely necessary to have same fields ? 具有相同的字段是否绝对必要?

Yes, if you don't want any surprises. 是的,如果您不希望有任何惊喜。

If yes, what if I don't use same field ? 如果是,如果我不使用同一字段怎么办?

You might get different hashCode for objects that are equal, as per equals() method, which is a requirement for the equals and hashCode contract. 根据equals()方法,对于相等的对象,您可能会获得不同的hashCode ,这是equals和hashCode契约的要求。

For example, suppose you've 3 fields - a , b , c . 例如,假设你已经3场- abc And you use a and b for equals() method, and all the 3 fields for hashCode() method. 然后,将ab用于equals()方法,并将所有3个字段用于hashCode()方法。 So, for 2 objects, if a and b are equals, and c is different, both will be equals with different hashcode. 因此,对于2个对象,如果ab相等,并且c不同,则两者将具有不同的哈希码。

Will it affect HashMap performance or HashMap Accuracy ? 它会影响HashMap的性能还是HashMap的准确性?

It's not about performance, but yes your map will not behave as expected. 这与性能无关,但是是的,您的地图将无法达到预期的效果。

The fields don't have to be the same. 字段不必相同。 The requirement is for two objects that are equal, they must have the same hash code. 要求两个对象相等,它们必须具有相同的哈希码。 If they have the same hash code, they don't have to be equal. 如果它们具有相同的哈希码,则不必相等。 From the javadocs: 从javadocs:

  • 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. 在Java应用程序的执行过程中,只要在同一对象上多次调用它,则hashCode方法必须一致地返回相同的整数,前提是未修改该对象的equals比较中使用的信息。 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. 如果根据equals(Object)方法,两个对象相等,则在两个对象中的每个对象上调用hashCode方法必须产生相同的整数结果。
  • 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. 根据equals(java.lang.Object)方法,如果两个对象不相等,则不需要在两个对象中的每个对象上调用hashCode方法必须产生不同的整数结果。 However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables. 但是,程序员应该意识到,为不相等的对象生成不同的整数结果可能会提高哈希表的性能。

For example, you could return 1 as your hash code always, and you would obey the hash code contract, no matter what fields you used in your equals method. 例如,无论您在equals方法中使用什么字段,都可以始终返回1作为哈希码,并且遵守哈希码协定。

Returning 1 all the time would improve the computation time of hashCode, but HashMap's performance would drop since it would have to resort to equals() more often. 始终返回1可以改善hashCode的计算时间,但是HashMap的性能会下降,因为它不得不更频繁地使用equals()。

Fields used in hashcode can be a subset of fields used in equals. 哈希码中使用的字段可以是等于中使用的字段的子集。 It will still abide by this rule "Whenever a.equals(b), then a.hashCode() must be same as b.hashCode()" 它仍将遵守此规则“只要a.equals(b),则a.hashCode()必须与b.hashCode()相同”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM