Why hashCode must be the same for two objects equal to each other?
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