The value of a hash is speed. We use arrays to hold key information, which is not the key itself, but rather generates a number (hash code) from the key objectas an array subscript. Because the capacity of the array is fixed, and the size of the hash container is variable, different keys can produce the same array subscript (hash code). That is, there may be conflicts (and, of course, exceptions, such as Enummap and Enumset). Therefore, the value of the array holds a list (reference) that holds the values of all the same hash codes. The values in the list are then linearly queried using equals. If the hash function is well designed, the array has fewer values at each location, and a small amount of wasted space. Therefore, the query process is to calculate the key hash code to get the array subscript, and then memory addressing (time complexity of O (1), assignment) to find the value of the array, and then traverse the list (time complexity O (n), linear query) can be. That is, hashcode and equals together determine the uniqueness of the object.
All classes inherit from object. The hash code generated by the Hashcode method of object is, in fact, used to calculate the hash code by default , and the Equals method is actually the address comparison (= =). Obviously, when we use hash containers (such as HashMap's key,hashset, etc.), we also inherit the hashcode and equals of object in our custom class. you must override the Hashcode and Equals methods. a good hashcode () should produce a hash code that is evenly distributed. Can be generated automatically with the IDE. Here is an example:
1 Importjava.util.List;2 3 Public classTest9 {4 5 BooleanA;6 byteb;7 ShortC;8 intD;9 Chare;Ten LongF; One floatG; A Doubleh; - String i; -List<string>J; the int[] k; - - @Override - Public inthashcode () { + //the magic number in [STEP1] hashcode (), 31 is chosen because it is an odd prime, and if the multiplier is even, and the multiplication overflows, the information is lost because multiplying with 2 is equivalent to the shift operation. - //the benefits of using prime numbers are not obvious, but it is customary to use prime numbers to calculate hash results. 31 There is a good feature, that is, using shift and subtraction instead of multiplication, you can get better performance: 31*i== (i<<5)-I. Today's VMs can automatically do this optimization. (from "effective Java") + Final intPrime = 31; A // [STEP2] calculates the hash code for each meaningful field in the object with the following formula result = Prime * result + C at intresult = 1; - //Boolean -result = Prime * result + (a? 1231:1237); - //Byte/short/int/char -result = Prime * result +b; -result = Prime * result +C; inresult = Prime * result +D; -result = Prime * result +e; to //Long +result = Prime * result + (int) (f ^ (F >>> 32)); - //float theresult = Prime * result +Float.floattointbits (g); * //Double $ Longtemp;Panax Notoginsengtemp =double.doubletolongbits (h); -result = Prime * result + (int) (temp ^ (temp >>> 32)); the //Object +result = Prime * result + ((i = =NULL) ? 0: I.hashcode ()); A //List (requires each element to implement Hashcode) theresult = Prime * result + ((j = =NULL) ? 0: J.hashcode ()); + //Array (requires each element to implement Hashcode) -result = Prime * result +Arrays.hashcode (k); $ // [STEP3] return $ returnresult; - } - @Override the Public Booleanequals (Object obj) { - if( This==obj)Wuyi return true; the if(obj = =NULL) - return false; Wu if(GetClass ()! =Obj.getclass ()) - return false; AboutTest9 other =(Test9) obj; $ if(A! =other.a) - return false; - if(b! =other.b) - return false; A if(c! =other.c) + return false; the if(d! =OTHER.D) - return false; $ if(E! =OTHER.E) the return false; the if(F! =other.f) the return false; the if(Float.floattointbits (g)! =float.floattointbits (OTHER.G)) - return false; in if(Double.doubletolongbits (h)! =double.doubletolongbits (other.h)) the return false; the if(i = =NULL) { About if(Other.i! =NULL) the return false; the}Else if(!i.equals (other.i)) the return false; + if(J = =NULL) { - if(OTHER.J! =NULL) the return false;Bayi}Else if(!j.equals (OTHER.J)) the return false; the if(!arrays.equals (k, other.k)) - return false; - return true; the } the}
Java Collection (7): hash and hash codes