Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Why hashCode must be the same for two objects equal to each other?

hashcode objects
0
Posted

Why hashCode must be the same for two objects equal to each other?

0

This is a complicated question. Let us discuss it slowly. • You must read the javadoc here first. Otherwise, we have no common ground. It is excellent reading too! • HashTable or HashMap are fundamental data structure in computer science. The lookup speed is close to O(1), since we use hashCode to put (store) and look-up (retrieve) elements. See more detailed explanation on HashTable here! • Equal objects must have the same hashCode! It is a must, not a choice. Otherwise, HashTable or HashMap will not work correctly! • Different/unequal objects have different hashCodes or not, which has everything to do with the efficiency of HashTable/HashMap etc. It is a trade-off between time and space. Less collision will make look-up faster (saving time), but will use more memory (wasting space). • The hashCode generation/look-up algorithm usually considers the sparcity of the hashTable to make the decision. However, since memory is so cheap now, people no longer pay too much attention to the algo

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123