Explain the essential difference and relationship between hashcode () and equals ()

In the process of learning Java and making examples according to videos, I have a deeper understanding of the two methods of equals and hashcode, mainly because they are easy to be confused and easy to make mistakes. I also learn and share them with you through online materials

Equals() method

Equals is one of the methods provided by the object class. As we all know, every Java class inherits from the object class, so every object has the equals method. When we use this method, we usually rewrite this method. Why?

First look at the source code of the equals () method in an object class:

From this method, we can see that equals () returns true only when an instance is equal to itself. Generally speaking, the comparison is whether the two references point to the same object in memory, or whether the instances are equal. When we use equals () to compare two references to value objects, we often want to know whether they are logically equal, rather than whether they point to the same object -- that's why we usually rewrite this method.

Description the equals () method has been overridden in the string class. If the equals () method is not overridden, S1 Equals (S2) compares whether the memory addresses pointed to by the two objects are the same by default. The return value must be false.

Of course, next we will rewrite the equals () method, which must comply with the general convention. From Java The equals method implements the equivalence relationship according to the specification of lang. object. The following five points are required to be followed:

1. Reflexivity: for any reference value x, x.equals (x) must be true.

2. Symmetry: for any reference value x and y, when x.equals (y) returns true, y.equals (x) must also return true.

3. Transitivity: for any reference value x, y and Z, if x.equals (y) returns true and y.equals (z) returns true, then x.equals (z) must also return true.

4. Consistency: for any reference value x and y, if the object information used for equals comparison has not been modified, calling x.equals (y) multiple times will either consistently return true or consistently return false. 5. Non null: for any non null reference value x, x.equals (null) must return false.

Hashcode() method

The hashcode () method is also inherited from the object class. In the object class, it is defined as follows:

Hashcode () returns the hash code value of the object, which is usually an integer converted from the internal address of the object. Its implementation is mainly to improve the performance of the hash table (such as the hash table provided by java.util.hashtable).

It must be borne in mind that in every class that overrides the equals method, you must also override the hashcode method. If you do not do so, you will violate object The general convention of hashcode, which makes this class unable to work properly with all hash based collection classes.

The relationship between the return value of hashcode() and equals() is as follows:

If x.equals (y) returns "true", the hashcode () of X and y must be equal.

If x.equals (y) returns "false", the hashcodes () of X and Y may be equal or unequal.

Detailed analysis

Equals() is to judge whether two sets are equal [provided that equals() is overridden in the class]== Determines whether reference values point to the same object.

1. When adding an object to the set, first calculate the hashcode code of the object to be added, and get a location to store the current object according to the value. If no object exists in the location, the set considers that the object does not exist in the set and adds it directly. If there is an object in this location, then compare the object to be added to the collection with the object in this location with the equals method. If the equals method returns false, the collection considers that the object does not exist in the collection, conduct another hash, and put the object into the new address calculated after the hash. If the equals method returns true, Then the collection considers that the object already exists in the collection and will not add the object to the collection.

2. When overriding the equals method, you must override the hashcode method. In the Java collection, the rule to judge whether two objects are equal is:

1) , judge whether the hashcodes of two objects are equal

If they are not equal, it is considered that the two objects are not equal, and it is over; If equal, go to 2

2) , judge whether two objects are equal by equals operation

If they are not equal, the two objects are not equal

If equal, two objects are considered equal (equals () is the key to judge whether two objects are equal)

It can be seen that when hashcode () is equal, the equals () method may also be unequal.

This is because the string class has overridden the equals () method and the hashcode () method.

But look at the following procedure:

The output result is:

Why does HashSet add equal elements?

Is this contrary to the HashSet principle? Answer: No, because different hash code values are generated when comparing the newstudent (1, "Zhangsan") objects created twice according to hashcode (), HashSet treats them as different objects. Of course, the values returned by the equals () method are also different. So why generate different hash code values? The reason is that the student class we wrote didn't reset its hashcode () and equals () methods

Therefore, when comparing, it is the hashcode () method in the inherited object class. It is a local method. It compares the address (reference address) of the object. When using the new method to create the object, of course, different objects are generated twice. The result is that the values returned by the hashcode () of the two objects are different. So how to solve this problem?

The reason is that the hashcode () and equals () methods are renamed in the student class.

According to the rewritten method, even if new student (1, "Zhangsan") is called twice, when we obtain the hash code of the object, the hash code obtained according to the rewritten method hashcode () must be the same. So when we run the modified program, we will see that the problem of duplicate elements has been eliminated.

summary

This knowledge is easy to make mistakes. Understanding must be profound. A lot of practice will have a deeper understanding of principles and definitions. If you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>