In depth understanding of hashcode method in Java
For hashcode, Wikipedia:
Hashcode is to extract a 32-bit integer based on all the data stored in an object instance. The purpose of the integer is to mark the uniqueness of the instance. Similar to MD5 code, each file can generate a unique MD5 code through MD5 algorithm. However, the hashcode in Java is not really implemented to generate a unique hashcode for each object, and there will still be a certain repetition probability.
Let's take a look at the object class. We know that the object class is the direct or indirect parent class of all classes in the Java program, which is at the highest point of the class level. Many common methods are defined in the object class, including the hashcode method we want to talk about, as follows
Note that there is a native modifier in front of the hashcode method, which means that the hashcode method is implemented by a non Java language. The specific method is implemented externally and returns the address of the memory object.
The equals and hashcode methods are rewritten in many classes in Java. Why? The most common string class, for example, I define two strings with the same characters. When comparing them, the results I want should be equal. If you don't rewrite the equals and hashcode methods, they will certainly not be equal, because the memory addresses of the two objects are different.
In fact, this code is the implementation of this mathematical expression
S [i] is the ith character of string, and N is the length of string. Then why use 31 instead of other numbers? Effective Java says that 31 is selected because it is an odd prime. If the multiplier is even and the multiplication overflows, the information will be lost, because multiplying with 2 is equivalent to shift operation. The benefits of using prime numbers are not obvious, but it is customary to use prime numbers to calculate hash results. 31 has a good feature, which is to use shift and subtraction instead of multiplication to get better performance: 31 * I = = (I < < 5) - I. Today's VMS can automatically complete this optimization.
You can see that the string class uses its value value as a parameter to calculate the hashcode, that is, the same value must have the same hashcode value. This is also easy to understand. If the values are the same, the equals comparison is also equal. If the equals method is equal, the hashcode must be equal. The reverse is not necessarily true. It does not guarantee that the same hashcode must have the same object.
A good hash function should be like this: generate unequal hashcodes for different objects.
Ideally, the hash function should evenly distribute unequal instances in the set to all possible hashcodes. It is very difficult to achieve this ideal situation, at least Java does not. Because we can see that hashcodes are non randomly generated, and it has a certain law, that is, the above mathematical equation. We can construct some hashcodes with the same hashcode but different value values, for example, the hashcodes of AA and BB are the same.
The following code:
Output results:
Generally, when rewriting the equal function, you also need to rewrite the hashcode function. Why?
Taking a look at this example, let's create a simple class employee
The above employee class only has some very basic properties and getters and setters Now consider a situation where you need to compare two employees.
There is no doubt that the above program will output false, but in fact, the above two objects represent an employee. The real business logic wants us to return true.
To do this, we need to override the equals method.
Add this method to the above class, and eauqlstest will output true.
So are we done? No, let's change the test method.
The output of the above program is two. If two employee objects equals return true, only one object should be stored in the set. What's the problem?
We forgot the second important method, hashcode (). As mentioned in the JDK Javadoc, if you override the equals () method, you must override the hashcode () method. We add the following method, and the program will execute correctly.
Things to remember
Try to use the same attribute of the object to generate hashcode () and equals () methods. In our case, we use the employee ID. The eqauls method must be consistent (equals should return the same value if the object has not been modified). Whenever a.equals (b), a.hashcode() must be equal to b.hashcode(). Both must be rewritten at the same time.
summary
The above is all about the in-depth understanding of the hashcode method in Java. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!