Let's briefly talk about the HashCode() and equals() methods

Let's briefly talk about the HashCode() and equals() methods


  • The existence of hashCode is mainly used for the quickness of search, such as Hashtable, HashMap, etc. hashCode is used to determine the storage address of objects in the three column storage structure.
  • If the two objects are the same, it is applicable to the euqals(java.lang.Object) method. Then the hashcodes of the two objects must be the same.
  • If the euqals method of the object is rewritten, the hashCode of the object should also be rewritten as much as possible, and the objects used in the hashCode must be consistent with those used in the equals method, otherwise the second point mentioned above will be violated.
  • If the hashcodes of the two objects are the same, it does not necessarily mean that the two objects are the same, that is, it does not necessarily apply to the equals() method. It can only indicate that the two objects are in a three column storage structure, such as Hashtable, They are in the same basket.
1.hashcode It is used for searching. If you have studied data structure, you should know that there are in the chapter of searching and sorting  
For example, there is such a location in memory  
0  1  2  3  4  5  6  7    
And I have a class that has a field called ID,I want to store this class in one of the above eight locations,
If not hashcode For arbitrary storage, you need to find one by one in these eight locations, or use algorithms such as dichotomy.  
But if you use hashcode That will improve efficiency a lot.  
We have a field in this class called ID,Then we define our hashcode by ID%8,
Then store our class in the location where we get the remainder. Like ours ID 9,
9 If the remainder of division 8 is 1, we will put the class in the position of 1,
If ID Is 13 and the remainder is 5, so we put this class in the position of 5.
In this way, you can find the class through ID Divide by 8 and find the remainder directly to the storage location.  
2.But if two classes have the same hashcode How to do that (let's assume that the above class ID Not unique),
For example, if the remainder of 9 divided by 8 and 17 divided by 8 is 1, is this legal,
Yes. So how to judge? It needs to be defined at this time equals Yes.  
That is, let's pass first hashcode To determine whether the two classes are stored in a bucket,
But there may be many classes in this bucket, so we need to pass it again equals To find the class we want in this bucket.  
So. Rewritten equals(),Why rewrite hashCode()And?  
Think about it. If you want to find something in a bucket, you must first find the bucket,
You don't rewrite hashcode()To find the bucket, light rewrite equals()What's the use  
package java.lang;

public class Object {

     * Returns the hash code value of the object.
     * This method is supported to improve the performance of hash tables, such as those provided by java.util.Hashtable
     * {@link java.util.HashMap}.
     * <p>
     * hashCode The conventional agreements are:
     * <ul>
     * <li>During the execution of a Java application, when the hashCode method is called multiple times on the same object,
     *     The same integer must be returned consistently, provided that the information used to compare objects with equals has not been modified.
     *     This integer does not need to be consistent from one execution of an application to another execution of the same application.
     * <li>If two objects are equal according to the equals(Object) method,
     *     Then calling the hashCode method on each of these two objects must generate the same integer result.
     * <li>If two objects are not equal according to the equals(java.lang.Object) method,
     *     Then calling the hashCode method on any of these two objects does not necessarily require different integer results.
     *     However, programmers should be aware that generating different integer results for unequal objects can improve the performance of hash tables.
     * </ul>
     * <p>
     * In fact, the hashCode method defined by the Object class does return different integers for different objects.
     * (This is generally achieved by converting the internal address of the object into an integer,
     * But the JavaTM programming language does not need this implementation skill.)
     * @return  A hash code value for this object.
     * @see     java.lang.Object#equals(java.lang.Object)
     * @see     java.lang.System#identityHashCode
    public native int hashCode();

Next, let's look at the following example:

public class MyClass {
 public static void main(String[] args) {
     HashSet books=new HashSet();
     books.add(new A());
     books.add(new A());
     books.add(new B());
     books.add(new B());
     books.add(new C());
     books.add(new C());
class A{
 //The equals method of class A always returns true, but does not override its hashCode() method
 public boolean equals(Object o) {
     return true;
class B{
 //The hashCode() method of class B always returns 1, but does not override its equals() method
 public int hashCode() {
     return 1;
class C{
 public int hashCode(){
     return 2;

 public boolean equals(Object o) {
     return true;
  • Even if two A objects return true through equals() comparison, HashSet still treats them as two objects. Even if the hashCode() return value of two B objects is the same, HashSet still treats them as two objects.
  • That is, when putting an object into a HashSet, if you need to override the equals() method of the corresponding class of the object, you should also override its hashCode() method. The rule is: if two objects return true through the equals() method comparison, the hashCode value of the two objects should also be the same.
  • If two objects return true through the comparison of euqals() method, but the hashCode() method of the two objects returns different hashCode values, this will cause the HashSet to save the two objects in different positions in the Hash table, so that both objects can be added successfully, which conflicts with the rules of the Set set.
  • If the hashCode() method of two objects returns the same hashCode value, but they return false through the equals() method, it will be more troublesome: because the hashCode values of two objects are the same, hashSet will try to save them in the same location, but it won't work (otherwise there will be only one object left), Therefore, in fact, multiple objects will be saved in a chain structure at this location; When hashSet accesses collection elements, it also locates quickly according to the hashCode value of the element. If more than two elements in the hashSet have the same hashCode value, the performance will be degraded.

equals(Object obj)

  • If a class does not override the equals(Object obj) method, it is equivalent to comparing two objects through = = that is, comparing whether the spatial addresses of the objects in memory are equal.
  • If the equals(Object ibj) method is overridden, compare the equality according to the content of the overridden method. If true is returned, it is equal, and false is not equal.

Reference link

Added by curlytail on Wed, 09 Feb 2022 08:25:01 +0200