I didn't understand the use of hashCode() for just comparing the objects.
I think to just compare the objects, overriding the equals() method is enough and no need to override the hashCode() method.
But If we want to store those objects in side HashSet and HashMap, overriding of both equals() and hashCode() is required.
Is that my understanding correct? or If not, please explain me the use of hashCode() to just compare the objects.
If you want just to test equality of objects then yes, technically hashCode is unnecessary.
However, overriding equals without overriding hashCode violates the contracts of both methods and should be avoided.
Junilu Lacar wrote: If you're really lazy (...)
If OP is really very lazy, why not have an IDE to generate it?