Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to rewrite the equals and hashCode methods correctly
How to rewrite the equals and hashCode methods correctly
if you overload equals, for example, based on the content of the object, and keep the implementation of hashCode unchanged, then it is likely that two objects are obviously "equal", but hashCode is different.

In this way, when you save one of them as a key in hashMap, hasoTable or hashSet, and then use "equal" to find another one as a key value to find them, you can't find them at all.

with HashMap, if the key is a user-defined class, you must override hashcode () and equals ().

for each object, an integer value (hashCode) can be generated by its hashcode () method. After being processed, the integer value will be used as an array subscript to store the Entry corresponding to the object (storing the object and its corresponding value). The equals () method is used when inserting values or queries in HashMap. When the hashCode corresponding to the inserted value or query value in HashMap is equal to the hash code in the array, the equality method will be used to compare whether the key values are equal, so if you want to use the self-built object as the key of HashMap, you must rewrite the hashcode and equals methods of the object. 2. hashcode () and equals () already exist? Why rewrite it? Why not just use the original one? In HashMap, if you want to compare whether keys are equal, you should use these two functions at the same time! Because the hashcode () method of the user-defined class inherits from the Object class, and its hashcode code is the default memory address, the comparison is not equal even if two objects with the same meaning are generated. For example, it is normal to understand that the two objects should be equal, but if you don't rewrite the hashcode () method, the comparison is not equal!

the comparison key in p>HashMap is like this. First, find the hashcode () of the key, and compare whether its values are equal. If they are equal, compare equals (). If they are equal, they are considered equal. If equals () are not equal, they are not equal. If you only rewrite hashcode () and don't rewrite equals () method, when comparing equals (), you will only see whether they are the same object (that is, compare memory addresses), so you must rewrite both methods together. The method used by HashMap to judge whether the keys are equal actually calls HashSet to judge whether the added elements are equal.

Quote a passage from someone else ~

Generally speaking, if you want to put an object of a class into a container, you usually have to rewrite the equals () method for it, so that they can compare the address value instead of the content value. In particular, if you want to put the objects of your class into the hash, you should rewrite the hashCode () method; To put it in an ordered container, you have to override the compareTo () method.

for two objects whose p>equals () are equal, hashcode () must be equal;

two objects whose p>equals () are not equal cannot prove that their hashcode () is not equal. In other words, two objects whose equals () methods are not equal, hashcode () may be equal. My understanding is caused by the conflict when the hash code is generated.

On the other hand: hashcode () is not equal, and equals () is not equal; Hashcode () is equal, equals () may be equal or unequal.