Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the differences between hashCode () and equals ()

Shulou Source: shulou.com Published: 2022-06-02 08:18:00 10月04日 Update

Editor to share with you what are the differences between hashCode () and equals (), I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

If the two objects hashCode () are the same, does equals () have to be true?

First of all, the answer is definitely not necessarily. At the same time, the reverse equals is not necessarily the same as true,hashCode.

Both the hashCode and equals methods of the class can be overridden, and the values returned are entirely self-defined.

HashCode () returns the hash code value of the object; equals () returns whether the two objects are equal.

There are some general agreements about the hashCode and equal methods:

1. Comparing two objects with equals () returns true, then the hashCode () method of the two objects must return the same result.

2. Comparing two objects with equals () returns false, which does not require the hashCode () method to return different values, but it is best to return different values to improve the performance of the hash table.

3. Override the equals () method. You must override the hashCode () method to ensure that the two objects hashcode return the same value when the equals method is equal.

The above is all the contents of the article "what are the differences between hashCode () and equals ()". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Method object two same article different content hash not much agreement reverse at the same time most general performance more best knowledge answer result Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Huawei Shulou Tech Info Shulou Information Docker