According to the document, the hashCode implementation by default returns an integer that is different for each object
As far as reasonably practical, the hashCode method defined by the Object class returns different integers for different objects. (Usually this is implemented by converting the internal address of the object to an integer, but this implementation
the method is not required by the JavaTM programming language.)
However, for some time you want the hash code to be the same for different objects that have the same value. for example
Student s1 = new Student("John", 18); Student s2 = new Student("John", 18); s1.hashCode() != s2.hashCode(); // With the default implementation of hashCode
This problem occurs if you use a hash data structure within the collection, such as HashTable, HashSet. Specifically for a collection like HashSet, you will get a duplicate element and break the Set contract.
PalmRobotZ Jun 10 '13 at 15:52 2013-06-10 15:52
source share