What happens if hashCode() method is not overridden?
If you don’t override hashcode() then the default implementation in Object class will be used by collections. This implementation gives different values for different objects, even if they are equal according to the equals() method.
Some collections, like HashSet, HashMap or HashTable use the hash code to store its data and to retrieve it. If you don’t implement hashcode() and equals() in a consistent manner, then they will not function properly.
Example :
MyObject a = new MyObject(“a”, 123,”something”);
MyObject b = new MyObject(“a”, 123,”something”);
a and b will have different hash codes.
According equals() and hashCode() contract, if two objects are equal by equals() method then they must return same hash Code.