• notice
  • Congratulations on the launch of the Sought Tech site

One of "Ten Why": Why override the equals method and havehcode method?

        Let’s not talk about overriding the equals method and hashCode method.Do you find that the class you define is very Will the equals method and hashCode method be overwritten? ——In this way, this seems to be an unpopular and irrelevant knowledge, but it is also an indispensable piece of the puzzle in the Java foundation.

        In fact, the reason why rarely overrides the equals method and hashCode method is because it overrides equals The purpose of the method is to "find the same kind", it means that the objects in a class have the concept of "logical equality": that is, two objects may be created in different ways or timings, and they are two objects that exist separately in the heap memory., But when the values ​​of their member variables are exactly the same, it means that they are "logically equal."

        Only when we need to logically judge whether two objects are the same in our needs, it is necessary to use Override the equals method to determine whether two objects are equal.Otherwise, the default equals method is essentially to compare the values ​​of two reference types with "==".At this time, only their memory addresses are compared.Therefore: even if they are "logical Equal", the result of the equals method is all false.

        So, if I don’t need to use these methods to "find the same kind" at all, then I don’t need to bother with the equals method overwriting.

        Second, the object specification stipulates that if you want to override the equals method, you must also override the hashcode method-this is easy to understand, because when the result of the equals comparison is true , The hashCode method of the two objects must return the same integer (there is another sentence: when the result of equals comparison is false, the return value of the hashCode method of the two objects will usually be different, but it may be the same); and the default The hashCode method is the value converted according to the memory address.If the equals method is rewritten without rewriting the hashcode method, even if the result of the comparison of the two objects is true, because their memory addresses will not be the same, the result returned by the hashCode method It will not be the same (it is not clear how the bottom layer is converted, whether it is possible to encounter the same integer result in a small probability, it is not known for the time being), obviously does not conform to the specification, and the hashcode method must be rewritten to make it conform to the specification.

        Since the title prefix of this article is ten why, even if it is not a hundred thousand why, we should also ask the bottom: why we have the equals method to determine the equality of the objects, we Need a hashCode method?

        Obviously, the above mentioned is a specification, but the specification must have its own reasons, I think it is to use the hash algorithm in the tool class (such as HashMap) is easy to use, and establish this specification to facilitate the implementation of related tools.Take HashMap as an example.Its data structure is an array with linked list as its elements.After it is discretized according to the hash value, it can quickly assign positions to the object.When the discrete value is the same, the equals method is called to make a judgment-combining the two , Not only considers performance, but also ensures that the same objects will not be stored separately and cause errors.

        By the way, here is also a note: a good hashCode method should make two different objects return different integers as much as possible.Avoid hash collisions.However, despite this, because the implementation mechanism of HashMap does not directly use the integer returned by the hashCode method (see the source code for details), different hashes will still collide.Related issues derived from this, such as "how to set the size of the HashMap array to avoid array expansion", are beyond the scope of this article.

        In summary, the purpose of overriding the equals method and hashCode method is to "find "Similar", and also consider the performance of tools related to the hash algorithm.If the class you create does exist in the above situation, then overwrite and write it equals method and hashCode method are indispensable work, otherwise, they really have nothing to do with you, just forget it……

—— write by NOT IN【 Ignorance, ignorance, speechless, and heard ]


Technical otaku

Sought technology together

Related Topic


Leave a Reply