understand
The purpose of hashCode() is to get the hash code, also known as the hash code; it actually returns an int integer. The function of this hash code is to determine the index position of the object in the hash table.
hashCode() is defined in JDK's Object.java, which means that any class in Java contains the hashCode() function.
Although, each Java class contains a hashCode() function. However, the hashCode() of the class is only useful when creating and creating a "hash table" (see the description below for "hash table") (the function is to determine the position of each object of the class in the hash table; in other cases (for example, creating a single object of the class, or creating an array of objects of the class, etc.), the hashCode() of the class hashCode() has no effect.
The hash list above refers to: classes in Java collections that are essentially hash lists, such as HashMap, Hashtable, and HashSet.
In other words: hashCode() is only useful in hashlists, but is useless in other cases. The function of hashCode() in a hash table is to obtain the hash code of the object, and then determine the position of the object in the hash table.
We all know that a hash table stores key-value pairs, which are characterized by: it can quickly retrieve the corresponding "values" based on "keys". This uses hash code!
The essence of a hash table is implemented through an array. When we want to get a certain "value" in a hash table, we actually want to get the element at a certain position in the array. The position of the array is obtained by "key"; furthermore, the position of the array is calculated by the hash code corresponding to the "key".
Below, we will take HashSet as an example to explain in depth the role of hashCode().
Suppose there are already 1000 elements in the HashSet. What should I do when inserting the 1001th element? Because HashSet is a Set collection, it allows for duplicate elements.
"Compare the 1001th element one by one with the previous 1000 elements"? Obviously, this efficiency is equally inefficient. The hash table solves this problem very well. It calculates the position of the element in the hash table based on the hash code, and then inserts the element into that position. For the same element, naturally, only one is saved.
From this we can see that if two elements are equal, their hash codes must be equal; but the other way around is not. In a hash table,
1. If two objects are equal, then their hashCode() values must be the same;
2. If the two objects hashCode() are equal, they are not necessarily equal.
Note: This is the case in hash tables. This must be true in non hash lists!
Example
Let's take a look at a specific example.
public class HashTest { private int i; public int getI() { return i; } public void setI(int i) { this.i = i; } public int hashCode() { return i % 10; } public final static void main(String[] args) { HashTest a = new HashTest(); HashTest b = new HashTest(); a.setI(1); b.setI(1); Set<HashTest> set = new HashSet<HashTest>(); set.add(a); set.add(b); System.out.println(a.hashCode() == b.hashCode()); System.out.println(a.equals(b)); System.out.println(set); } }The result of this output:
true false [com.ubs.sae.test.HashTest@1, com.ubs.sae.test.HashTest@1]
In the above example, we just rewrite the hashCode method. From the above results, we can see that although the hashCodes of the two objects are equal, the two objects are actually not equal;, we did not rewrite the equals method, and then we will call the object's default equals method to compare whether the references of the two objects are the same, and it shows that these are two different objects, and the references of the two objects are definitely uncertain. Here we put the generated object in the HashSet, and only the unique object can be stored in the HashSet, that is, the same object (applicable to the equals method) will only store one, but here, there are actually two objects a and b that are placed in the HashSet, so the HashSet loses its own meaning.
At this time, we add the equals method:
public class HashTest { private int i; public int getI() { return i; } public void setI(int i) { this.i = i; } <span style="color:#3366FF;"><strong>public boolean equals(Object object) { if (object == null) { return false; } if (object == this) { return true; } if (!(object instanceof HashTest)) { return false; } HashTest other = (HashTest) object; if (other.getI() == this.getI()) { return true; } return false; }</strong></span> public int hashCode() { return i % 10; } public final static void main(String[] args) { HashTest a = new HashTest(); HashTest b = new HashTest(); a.setI(1); b.setI(1); Set<HashTest> set = new HashSet<HashTest>(); set.add(a); set.add(b); System.out.println(a.hashCode() == b.hashCode()); System.out.println(a.equals(b)); System.out.println(set); } }The results obtained at this time will be as follows:
true true [com.ubs.sae.test.HashTest@1]
From the results, we can see that the two objects are now completely equal, and only one object is stored in the HashSet.
Summarize
1. The existence of hashCode is mainly used for searching fastness, such as Hashtable, HashMap, etc. hashCode is used to determine the storage address of an object in the hash storage structure;
2. If the two objects are the same, it is applicable to the equals(java.lang.Object) method, then the hashCodes of these two objects must be the same;
3. If the object's equals method is rewritten, then the object's hashCode should also be rewritten as much as possible, and the object used by hashCode must be consistent with the same as the equals method, otherwise it will violate the second point mentioned above;
4. The hashCode of two objects is the same, which does not necessarily mean that the two objects are the same, that is, it does not necessarily apply to the equals(java.lang.Object) method. It can only indicate that these two objects are in a hash storage structure, such as Hashtable, and they are "stored in the same basket."