Why must also override the hashcode method when rewriting equals.

Source: Internet
Author: User

First, let's take a look at the source code of the string class: we can find that the string method overwrites the equals method of the object class, and also overwrites the hashcode method.

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;    }public int hashCode() {    int h = hash;    if (h == 0) {        int off = offset;        char val[] = value;        int len = count;            for (int i = 0; i < len; i++) {                h = 31*h + val[off++];            }            hash = h;        }        return h;    }

Why do we need to rewrite the equals method when rewriting the equals method:
First, the relationship between equals and hashcode is as follows:

1. If the two objects are the same (equals is used to compare and return true), their hashcode values must be the same;

2. If the hashcode of the two objects is the same, they are not necessarily the same (that is, false is returned for comparison with equals)

Self-understanding: the hashcode method is implemented to improve program efficiency, and the comparison of hashcode is performed first. If there are different hashcode methods, you do not need to compare equals, in this way, the comparison of equals is greatly reduced.

Number of times, which is obviously more efficient than the quantity to be compared. A good example is the use in the Set;

We all know that the list set in Java is ordered, so it can be repeated, and the Set set is unordered, so it cannot be repeated, so how can we ensure that we cannot put repeated elements? However, The equals method is the same for comparison.

If there are more than 10000 elements in the original collection, then put 10001 elements. Do you want to compare all the preceding elements to see if there are duplicates, this efficiency can be imagined, so hashcode

Java uses the hash table and uses the hash algorithm (also called the hash algorithm ), the object data is defined as an address based on the characteristics of the object using a specific algorithm, then the data defined later

As long as you check whether there is a value on the corresponding hashcode address, you can use equals for comparison. If not, you can directly insert it. As long as the equals usage is greatly reduced, the execution efficiency is greatly improved.

To continue with the above topic, why do we have to rewrite the hashcode method? Simply put, to ensure the same object and ensure that the hashcode value must be the same under the same equals, if equals is rewritten but not rewritten

Hashcode method, two irrelevant objects may have the same equals (because equal is rewritten based on the characteristics of the object), but the hashcode is indeed different.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.