Why do I have to override the hashCode method when I override equals?

First, let's take a look at the source code of the String class: it can be found that String overrides the equals method of the Object class, and also overrides the hashcode method.

copy code
copy code
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 ; } /*Return the hash code, the hash code of String is calculated as s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[ n-1] */ 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; }
copy code
copy code

So why do you have to rewrite the hashCode method when you rewrite the equals method:
First of all, the relationship between equals and hashcode is as follows:

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

2. If the hashCode of two objects is the same, they are not necessarily the same (that is, using equals comparison returns false)   

self-understanding:

Since the hashcode method is implemented in order to improve the efficiency of the program, the hashcode comparison is performed first. If it is different, then there is no need to perform the equals comparison, which greatly reduces the number of equals comparisons, which requires a large number of comparisons. The efficiency improvement is obvious, a good example is the use of collections;

We all know that the List collection in java is ordered, so it can be repeated, and the set collection is unordered, so it cannot be repeated, so how can we guarantee that it cannot be put into repeated elements, but rely on the equals method In the same comparison, if there are 10,000 elements in the original collection, then 10,001 elements are put in. Do you want to compare all the previous elements to see if there are any repetitions? Ouma, this efficiency can be imagined , so the hashcode was born, java uses the hash table, using the hash algorithm (also called the hash algorithm), is to use a specific algorithm to define the object data to an address according to the characteristics of the object, Then the data defined later only needs to see whether there is a value in the corresponding hashcode address, then use equals to compare, if not, insert it directly, as long as the number of equals usage is greatly reduced, the execution efficiency is greatly improved.

Continuing the above topic, why do you have to rewrite the hashcode method? In fact, it is simply to ensure that the same object is guaranteed to have the same hashcode value when the equals are the same. If you rewrite the equals without rewriting the hashcode method, you may There will be two unrelated objects with the same equals (because equals are rewritten according to the characteristics of the object), but the hashcodes are indeed different.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325993372&siteId=291194637