why is it suggested i also override `hashCode()` f...
# getting-started
h
why is it suggested i also override
hashCode()
for a class whenever i override
equals()
? I don't even know what the former does
r
The relationship between
hashcode
and
equals
is something you should know about when doing work on the JVM. The JavaDoc for those two methods is a good start. Most important: If
a.equals(b)
is true, then
a.hashCode()
and
b.hashCode()
have to return the same value. If you don't adhere to this contract, then things like HashMap and HashSet won't work properly.