Detailed explanation of equals() and hashcode()

equals()

It is used to determine whether other objects are equal to the object. The
equals() method is defined as follows in the object class:


public boolean equals(Object obj) {  
    return (this == obj);  
}

Need to pay attention to: String, Math, Integer, Double and other encapsulation classes have covered the equals() method of the object class when using the equals() method.
For example String class


public boolean equals(Object anObject) {  
    if (this == anObject) {  
        return true;  
    }  
    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;  
} 

Insert picture description here

hashcode()

The hashCode() method returns a hash code value to the object. This method is used for hash tables, such as HashMap.

First of all, to understand the role of hashCode, you must first know the collection in Java. In general, there are two types of collections in Java, one is List, and the other is Set. Do you know the difference between them? The elements in the former set are ordered, and the elements can be repeated; the elements in the latter are disordered, but the elements cannot be repeated. Then there is a more serious problem: if you want to ensure that the elements are not repeated, what should be judged on whether two elements are repeated? This is the Object.equals method. However, if the check is performed every time an element is added, when there are many elements, the number of comparisons of the elements added to the set will be very large. In other words, if there are already 1000 elements in the collection, when the 1001st element is added to the collection, it will call the equals method 1000 times. This will obviously greatly reduce efficiency. Therefore, Java uses the principle of hash tables. Hash algorithm is also called hash algorithm, which directly assigns data to an address according to a specific algorithm. If you explain the hash algorithm in detail, you need more article space, I won't introduce it here. Beginners can understand this way, the hashCode method actually returns the physical address of the object storage (actually it is not the real physical address in memory, but it can be understood this way). In this way, when a new element is added to the collection, the hashCode method of this element is called first, and it can be located at the physical location where it should be placed. If there is no element at this position, it can be directly stored at this position without any comparison; if there is already an element at this position, it will call its equals method to compare with the new element. If it is the same, it will not be stored. If it is not the same, hash other addresses. So there is a problem of conflict resolution. In this way, the number of actual calls to the equals method is greatly reduced, almost only once or twice. Therefore,
Java stipulates the eqauls method and hashCode method as follows:

1. If the two objects are the same, their hashCode values ​​must be the same;

2. If the hashCode of two objects are the same, they are not necessarily the same. The same object mentioned above refers to the comparison using the eqauls method. Of course you can do it as required, but you will find that the same objects can appear in the Set collection. At the same time, the efficiency of adding new elements will be greatly reduced.

If you rewrite the equal() method, the two instances that are not actually an object are logically equal, but the hashcode is not equal.
  
  So remember to rewrite the hashcode.
  
  What are the consequences of not rewriting it? Of course, for example, when you use hashmap, hashtable and other design hashcode classes, you will get into trouble.
  
  As for how to rewrite a hashcode, there are good and bad, depending on each person's skills. There are still dedicated people studying excellent hash algorithms.

In other words, List is an ordered and repeatable object container interface, and Set is an unordered, non-repeatable object container interface. Later, I will talk about how Set achieves non-repetition: In order to avoid the system burden caused by repeated use of the equal method, set first calls the hashCode method to detect whether it is occupied. If it is occupied, then calls the equal method to determine whether the occupied is the same.

The close relationship between Hashset, Hashmap, Hashtable and hashcode() and equals()

Focus on
https://www.cnblogs.com/Qian123/p/5703507.html#_label2

Guess you like

Origin blog.csdn.net/weixin_43722571/article/details/99969899