The following content is summarized in objective Java.
1. When to rewrite equals ()
When a class has its own unique concept of "logical equality" (different from the concept of object identity ).
2. design equals ()
[1] use the instanceof operator to check whether the real parameter is of the correct type ".
[2] for each "key field" in the class, check the field value in the real parameter and the corresponding field value in the current object.
[2.1] for non-float and double primitive type fields, use = for comparison;
[2.2] recursively calls the equals method for the object reference domain;
[2.3] for float domains, use Float. floatToIntBits (afloat) to convert to int, and then use = for comparison;
[2.4] for double fields, convert Double. doubleToLongBits (adouble) to int and then use = for comparison;
[2.5] For array fields, call the Arrays. equals method.
3. When you rewrite equals (), you always need to rewrite hashCode ()
According to the equals method of a class (after rewriting), two different instances may be logically equal. However, according to the Object. hashCode method, they are only two objects. Therefore, the violation of "equal objects must have equal hash codes ".
4. Design hashCode ()
[1] store a non-zero constant value, such as 17, in the result of the int variable;
[2] for each key field f in an object (each field considered in the equals method ):
[2.1] boolean type, computing (f? 0: 1 );
[2.2] byte, char, short type, calculation (int );
[2.3] long type, calculation (int) (f ^ (f >>> 32 ));
[2.4] float type, computing Float. floatToIntBits (afloat );
[2.5] double type, calculate Double. doubleToLongBits (adouble) to get a long value, and then execute [2.3];
[2.6] references an object and recursively calls its hashCode method;
[2.7] array field, which calls its hashCode method for each element.
[3] Save the hash code calculated above to int variable c, and then execute result = 37 * result + c;
[4] Return result.
5. Example
The following class follows the above design principles and overwrites the equals () and hashCode () of the class ().
Package com. zj. unit;
Import java. util. Arrays;
Public class Unit {
Private short ashort;
Private char achar;
Private byte abyte;
Private boolean abool;
Private long along;
Private float afloat;
Private double adouble;
Private Unit aObject;
Private int [] ints;
Private Unit [] units;
Public boolean equals (Object o ){
If (! (O instanceof Unit ))
Return false;
Unit unit = (Unit) o;
Return unit. ashort = ashort
& Amp; unit. achar = achar
& Amp; unit. abyte = abyte
& Amp; unit. abool = abool
& Amp; unit. along = along
& Float. floatToIntBits (unit. afloat) = Float
. FloatToIntBits (afloat)
& Double. doubleToLongBits (unit. adouble) = Double
. DoubleToLongBits (adouble)
& Amp; unit. aObject. equals (aObject)
& EqualsInts (unit. ints)
& Amp; compute sunits (unit. units );
}
Private boolean restart sints (int [] aints ){
Return Arrays. equals (ints, aints );
}
Private boolean published sunits (Unit [] aUnits ){
Return Arrays. equals (units, aUnits );
}
Public int hashCode (){
Int result = 17;
Result = 37 * result + (int) ashort;
Result = 37 * result + (int) achar;
Result = 37 * result + (int) abyte;
Result = 37 * result + (abool? 0: 1 );
Result = 37 * result + (int) (along ^ (along >>> 32 ));
Result = 37 * result + Float. floatToIntBits (afloat );
Long tolong = Double. doubleToLongBits (adouble );
Result = 37 * result + (int) (tolong ^ (tolong >>> 32 ));
Result = 37 * result + aObject. hashCode ();
Result = 37 * result + intsHashCode (ints );
Result = 37 * result + unitsHashCode (units );
Return result;
}
Private int intsHashCode (int [] aints ){
Int result = 17;
For (int I = 0; I <aints. length; I ++)
Result = 37 * result + aints [I];
Return result;
}
Private int unitsHashCode (Unit [] aUnits ){
Int result = 17;
For (int I = 0; I <aUnits. length; I ++)
Result = 37 * result + aUnits [I]. hashCode ();
Return result;
}
}