When Java override equals () method, why usually rewrite HashCode () method? ? ?

Why rewrite HashCode () method?

  • Check in advance using the hashcode method to avoid each level attained calls the equals method to improve the efficiency of

  • Guaranteed to be the same object, if you override the equals method, but does not override the hashcode method, the object will be equal equals, hashcode unequal situation, override hashcode method is to avoid this from happening.

  • Override equals () method is generally convenient for comparing two content objects are equal.

  • Hash code value hashCode () method returns the object invokes the method, this method returns the hash code value of type integer.

  • A class overrides the equals () method, usually it is necessary to rewrite hsahCode () method, the purpose is to safeguard the general contract of hashCode () method, is equal to the agreement declare objects must have equal hash codes.

    • During the Java application execution, on the same subject repeatedly calling the hashCode () method must consistently return the same integer, provided on an object equals () method used in the comparative information has not been modified. One execution of an application to another execution of the same application from the integer need not remain consistent.
    • If according equals (Object) method, two objects are equal, then calling the hashCode on each of the two objects () method must produce the same integer result.
    • Is not required in the following cases: if according equals (java.lang.Object) method, two objects are not equal, then calling the hashCode on either of the two objects () method must produce results different integers, it will be understood that producing distinct integer results for unequal objects may improve the performance of the hash.
    • Defined by the Object class hashCode () method does return different integers for different objects.

Note :

  • Equal objects must have the same hash code, whereas the object is not necessarily the same hash code is equal to, not equal and do not necessarily need to have the object of different hash codes.
  • Use hashCode hash code based on the set required () method returns the hash value is stored and managed element, set in use, first determine which storage location based on the hash value of the element code of the object, and then under the equals () method Analyzing Results if the element object already exists, and finally performs different processing according to the determination result. Therefore, the practical application if you override the equals () method, then hashCode () method will be overridden.
  • hash value returned is of type int and prevent overflow.
  • Different objects returned hash value should be sufficiently different.
  • Design hashCode (when the most important) factor is: whenever, calling hashCode the same object () should produce the same value. If an object is generated when the talk HashMap with add put () a hashCdoe value, with get () hashCode removed Shique raises another value, then it can not obtain this object a. So if you hashCode method relies on the variable data object, the user must be careful, because this data changes, hashCode () method will generate a different hash codes
Published 29 original articles · won praise 33 · views 5123

Guess you like

Origin blog.csdn.net/lxn1214/article/details/104626375