From https://www.cnblogs.com/szlbm/p/5504623.html Preface
Object provides us with a native method of "public native int hashcode ();", this article is about what the hash is and the role of the hashcode hash
Let's start with a picture and see what a hash is.
Hash is the meaning of hashing, is the arbitrary length of input, through the hashing algorithm to the fixed length of output, the output is hash value. For hash values, there are several key conclusions:
1. If there are records in the hash table that are equal to the hash original input k, then K must be in the storage location of F (k)
2, the different keywords after hashing algorithm transformation may get the same hash address, this phenomenon is called collision
3, if two hash values are different (the premise is the same hash algorithm), then these two hash values corresponding to the original input must be different hashcode
Then talk about what is hashcode and summarize several key points:
1, the existence of hashcode is mainly to find the shortcut, Hashcode is used in the hash storage structure to determine the storage address of the object
2, if two objects equals equal, then the hashcode of the two objects must also be the same
3, if the object's Equals method is overridden, then the object's Hashcode method also tries to rewrite
4, if two objects of the same hashcode, does not mean that two objects are the same, only to show that these two objects in the hash storage structure, stored in the same location hashcode What's the use
Back to the most critical question, what is the use of hashcode. Give an example:
1, assuming that there are 0 1 2 3 4 5 6 7 8 in memory, if I have a field called ID, then I want to put this field in one of the above 8 locations, if not hashcode and arbitrary storage, then when the search will need to go to 8 places to find
2, the use of hashcode will be more efficient, the ID of the hashcode%8, and then the ID stored in the location to obtain the remainder, and then every time you look at the class can be found by the ID of the hashcode%8 to find the remainder directly to the location of the store
3, if the ID of the hashcode%8 figure out the position itself already have data how to do. This depends on the implementation of the algorithm, such as threadlocal in the practice is from the calculated position to find the first empty position, the placement of data; HashMap's approach is to connect by chain structure. Anyway, as long as the guarantee to put the time and take the algorithm consistent on the line.
4, if the ID of the hashcode%8 equal how to do (this corresponds to the 3rd of the chain-like structure of the scene). This is the time to define equals. First, the hashcode%8 is used to determine which position the class is in, and then to find the desired class in this position through equals. Compared to the two-class time is also similar, first through hashcode comparison, if the hashcode equality and then judge equals. If the hashcode of the two classes are different, then these two classes must be distinct.
Give a real example set. We know that the elements in set are not repeatable, so how to do it. Set is based on the Equals () method to determine whether two elements are equal. For example, there are already 1000 elements in the set, then the 1001th element comes in, it is possible to call the Equals method 1000 times, if the Equals method is written in a complex way, the comparison is very much, then the efficiency will be greatly reduced. The use of hashcode is not the same, such as HashSet, the bottom is based on the HashMap implementation, first through the hashcode to take a model, so that all of a sudden fixed to a position, if there is no element in this position, Then you can be sure that the hashset must not and the elements of the new element equals, you can directly store, do not need to compare; if there are elements in this position, compare each one, compare the time before comparing Hashcode,hashcode are different, and certainly not. Like, Hashcode equal, and then equals comparison, there is no same element to save, there are the same elements do not exist. If the original set has the same element, as long as the hashcode generation is defined well (not repeated), no matter how many elements there are in the set, only the equals can be executed once. As a result, the number of actual calls to the Equals method is greatly reduced and efficiency is increased. Why rewrite object's equals (Object obj) method to rewrite the object's Hashcode () method as much as possible
When we rewrite the Equals (object obj) method of object, we should rewrite the Hashcode () method as much as possible, for a reason, as explained in detail below:
public class Hashcodeclass {private String str0;
Private double dou0;
private int int0; public boolean equals (Object obj) {if (obj instanceof hashcodeclass) {Hashcodeclass HCC
= (hashcodeclass) obj; if (Hcc.str0.equals (this.str0) && hcc.dou0 = this.dou0 && hcc.int0 = th
IS.INT0) {return true;
return false;
return false; }
}
public class Testmain
{public
static void Main (string[] args)
{
System.out.println (new Hashcodeclass (). Hashcode ());
System.out.println (New Hashcodeclass (). Hashcode ());
System.out.println (New Hashcodeclass (). Hashcode ());
System.out.println (New Hashcodeclass (). Hashcode ());
System.out.println (New Hashcodeclass (). Hashcode ());
System.out.println (New Hashcodeclass (). Hashcode ());
}
The values printed are:
1901116749
1807500377
355165777
1414159026
1569228633
778966024
The premise of two hashcodeclass equals is that two Hashcodeclass str0, dou0, and int0 are equal respectively. OK, then this class does not rewrite the Hashcode () method is problematic.
Now my hashcodeclass are not assigned an initial value, then these 6 hashcodeclass should be all equals. If you take hashset as an example, the size of the HashMap table itself in HashSet is 16, then the 6 hashcode 16 modulo 13, 9, 1, 2, 9, 8, respectively. The first place to put the table[13], the second place to put the table[9], and the third place to put table[1] ... But obviously is all equals 6 hashcodeclass, how can do this. HashSet itself requires that the Equals object not be duplicated, and that 6 equals objects now have 5 in the collection (because two of the computed models are 9).
So what should we do? Rewrite the Hashcode method, according to STR0, Dou0, int0 an algorithm to generate a single as far as possible hashcode, so that the STR0, dou0, int0 are equal two hashcodeclass their hashcode is equal, This is why overriding the Equals method must try to rewrite the Hashcode method. Take a look at some of the classes in the JDK and do this:
integer of
public int hashcode () {return
value;
}
public boolean equals (Object obj) {
if (obj instanceof Integer) {return
value = = ((Integer) obj). Intvalue ()
; return
false;
}
So in integer, the hashcode of the variable and the value of the variable are the same
string of
public int hashcode () {int h = hash;
if (h = = 0) {int off = offset;
Char val[] = value;
int len = count;
for (int i = 0; i < len; i++) {h = 31*h + val[off++];
hash = h;
return h;
public boolean equals (object AnObject) {//If the same object, returns True if (this = = AnObject) {return true;
//If the object being compared is a string type if (AnObject instanceof string) {string anotherstring = (String) anobject;
int n = count;
if (n = = anotherstring.count) {char v1[] = value;
Char v2[] = Anotherstring.value;
int i = offset;
int j = Anotherstring.offset;
while (n--! = 0) {if (v1[i++]!= v2[j++]) return false;
return true;
return false; }
In string, the same string has the same hashcode, but objects created in different ways are not the same using the Equals method.
entity classes in HashMap entry
Public final int hashcode () {return
(key==null 0:key.hashcode ()) ^
(value==null? 0:value.hashcode ()); c4/>} public
final Boolean equals (Object o) {
if (!) ( o instanceof Map.entry) return
false;
Map.entry e = (map.entry) o;
Object K1 = Getkey ();
Object K2 = E.getkey ();
if (k1 = = K2 | | (K1!= null && k1.equals (K2))) {
Object v1 = GetValue ();
Object v2 = E.getvalue ();
if (v1 = = V2 | | (v1!= null && v1.equals (v2)))
return true;
}
return false;
}