1. when to rewrite equals ()
When a class has its own unique "logical equality" concept (different from the concept of object identity).
2.
Design
equals ()[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 rewriting
equals ()
, 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.
Design
hashcode ()[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.
Example
import java.util.Arrays;/** class Description: The Set collection filters out duplicate data for string types and 8 base data types, * * If you are storing other types of objects, you need to override the Hashcode method and the Equals method, and when equals is equal, it will compare the hashcode value * * If the value of the hashcode is consistent, it will not be stored in the set*/ Public classUnit {Private ShortAshort; Private CharAchar; Private byteAbyte; PrivateBoolean abool; Private Longalong; Private floatafloat; Private Doubleadouble; PrivateUnit Aobject; Private int[] ints; Privateunit[] Units; Publicboolean equals (Object o) {if(O = =NULL|| !(o instanceof Unit))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)&&arrays.equals (INTs, unit.ints)&&arrays.equals (units, unit.units); } Public intHashcode () {finalintPrime =Panax Notoginseng; intresult = -; Result= Prime * result + (int) Ashort; Result= Prime * result + (int) Achar; Result= Prime * result + (int) Abyte; Result= Prime * result + (Abool?0:1); Result= Prime * result + (int) (along ^ (along >>> +)); Result= Prime * result +float.floattointbits (afloat); LongTolong =double.doubletolongbits (adouble); Result= Prime * result + (int) (tolong ^ (Tolong >>> +)); Result= Prime * result +Aobject.hashcode (); Result= Prime * result +Intshashcode (ints); Result= Prime * result +Unitshashcode (units); returnresult; } Private intIntshashcode (int[] aints) { intresult = -; for(inti =0; i < aints.length; i++) Result=Panax Notoginseng* result +Aints[i]; returnresult; } Private intUnitshashcode (unit[] aunits) {intresult = -; for(inti =0; i < aunits.length; i++) Result=Panax Notoginseng* result +Aunits[i].hashcode (); returnresult; }}
java rewrite equals () and Hashcode ()