Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Why do you want to override the equals and HashCode methods?
Why do you want to override the equals and HashCode methods?
If you overload equals, such as object-based content, and keep the implementation of hashCode unchanged, then it is likely that the 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 the other as a key value to find them, you can't find them at all.

For HashMap, if the key is a custom class, you must override hashcode () and equals ().

For each object, an integer value (hashcode) can be generated by its hashCode () method. The processed integer value will be used as the entry corresponding to the array subscript storage object (storage object and its corresponding value). Use the equals () method when inserting values or queries into 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 use the original one? In HashMap, if you want to compare whether the keys are equal, you must 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, even if two objects with the same meaning are generated, the comparison is not equal. For example, it is normal to understand that two objects should be equal, but if you don't rewrite the hashcode () method, the comparison will not be equal!

The comparison key in 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 not the equals () method, then when comparing equals (), you will only see whether they are the same object (that is, comparing memory addresses), so you must rewrite the two methods together. HashMap's method of judging whether the keys are equal is actually to call HashSet to judge whether the added elements are equal.

Quote a passage from others ~

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

For two objects with equal equals (), Hashcode () must be equal;

Two objects whose 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 contrary: hashcode () is not equal, equals () is not equal; Hashcode () is equal, and equals () can be equal or not.

My understanding is, ha,