Why must also override the hashcode method share when overriding equals

  • 2020-04-01 02:22:38
  • OfStack


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;
    }

So why override equals when you override equals:
First of all, the relationship between equals and hashcode is like this:

1. If two objects are the same (that is, the equals comparison returns true), then their hashCode value must be the same;

2. If two objects have the same hashCode, they are not necessarily the same (that is, equals comparison returns false)& PI;  

Self-understanding: since the hashcode method is implemented in order to improve the efficiency of the program, the comparison of hashcode is carried out first. If not, the equals comparison is not necessary, which greatly reduces the equals comparison

The number of times, the number of comparisons that need to be made and the efficiency improvements that are obvious, a good example of that is the use in sets;

We all know that the List collection in Java is ordered and therefore repeatable, while the set collection is unordered and therefore unrepeatable, so how can we guarantee that it cannot be put into duplicate elements, but it is compared according to equals method

So, if you have 10,000 more elements in the original set, then you put in 10,001 elements, and you're going to compare all the previous elements to see if there's any overlap, omega, which you can imagine, so hashcode

As a result, Java USES a hash table, which USES a hash algorithm (also known as a hash algorithm) to define an object's data to an address based on its characteristics using a specific algorithm, and then defines the incoming data later

As long as there is no value on the corresponding hashcode address, equals is used to compare, if there is no value, then directly inserted, as long as the number of equals usage is greatly reduced, the execution efficiency is greatly improved.

Continuing with the above topic, why do I have to override the hashcode method, which is simply to ensure that the same object, the hashcode value must be the same if equals is the same, if equals is overridden and not overridden

The hashcode method might have two unrelated objects with the same equals (because equal is overridden based on the object's characteristics), but hashcode is different


Related articles: