The following summarizes from the effective Java.1.when to rewriteequals ()When a class has its own unique "logical equality" concept (different from the concept of object identity).2.Designequals ()[1] Use the instanceof operator to check if the argument is the correct type. [2] For each "critical field" in the class, check the field in the argument and the corresponding field value in the current object. [2.1] for non-float and double type of primitive type field, use = = comparison, [2.2] for the object reference domain, recursive call to the Equals method; [2.3] for the float domain, float is used.floattointbits(afloat) to int, and then = = comparison; [2.4] for double fields, double is used.doubletolongbits(adouble) to int, and then = = comparison; [2.5] for array fields, call the Arrays.equals method.3.when rewritingequals (), I always have to rewrite it.hashcode ()Depending on the Equals method of a class (rewritten), two distinct instances may be logically equal, but, according to the Object.hashcode method, they are only two objects. Therefore, the "equal hash code must be equal" is violated.4.Designhashcode ()[1] A non-0 constant value, such as 17, is stored in the int variable result; [2] For each key field F in the object (referring to each domain considered in the Equals method): [2.1]boolean type, calculation (f. 0:1); [2.2]byte,char,short type, calculation (int); [2.3]long type, calculation (int) (f ^ (f>>>32)); [2.4]float type, calculate float.floattointbits(afloat); [2.5]double type, computes double.doubletolongbits(adouble) Get a long, then execute [2.3]; [2.6] Object reference, recursive invocation of its Hashcode method; [2.7] An array field that invokes its Hashcode method on each of these elements. [3] The above computed hash code is saved to the INT variable C, and then executed Result=37*result+c; [4] returns result.5.ExampleThe following class follows the design principles above, overriding the Equals () and hashcode () of the class. PackageCom.zj.unit;ImportJava.util.Arrays; Public classUnit {Private ShortAshort;Private CharAchar;Private byteAbyte;Private BooleanAbool;Private Longalong;Private floatAfloat;Private Doubleadouble;PrivateUnit Aobject;Private int[] ints;PrivateUnit[] Units; Public BooleanEquals (Object o) {if(! (OinstanceofUnit))return false; Unit unit = (unit) o;returnUnit.ashort = = Ashort && Unit.achar = = Achar && unit.abyte = abyte && Unit.abool = = Abool && Unit.along = = along && Float.floattointbits(unit.afloat) = = Float.floattointbits(afloat) && Double.doubletolongbits(unit.adouble) = = Double.doubletolongbits(adouble) && unit.aObject.equals (aobject) && equalsints (unit.ints) && equ Alsunits (unit.units); }Private BooleanEqualsints (int[] aints) {returnArrays.equals(INTs, aints); }Private BooleanEqualsunits (unit[] aunits) {returnArrays.equals(units, aunits); } Public intHashcode () {intresult = 17; result = PNS * result + (int) Ashort; result = PNS * result + (int) Achar; result = PNS * result + (int) Abyte; result = PNS * result + (Abool 0:1); result = PNS * result + (int) (along ^ (along >>> 32)); result = PNS * result + Float.floattointbits(afloat);LongTolong = Double.doubletolongbits(adouble); result = PNS * result + (int) (tolong ^ (Tolong >>> 32)); result = PNS * result + aobject.hashcode (); result = PNS * result + intshashcode (ints); result = PNS * result + unitshashcode (units);returnResult }Private intIntshashcode (int[] aints) {intresult = 17; for(inti = 0; i < aints.length; i++) result = PNS * result + aints[i];returnResult }Private intUnitshashcode (unit[] aunits) {intresult = 17; for(inti = 0; i < aunits.length; i++) result = PNS * result + aunits[i].hashcode ();returnResult }}
Java: Overriding Equals () and Hashcode ()