I've read all the posts in the topic and I still have confusion on the following: when overriden and collision can happen? From what I'v read I see:
- Whenever two objects are the same in terms of
equals()
method, their hash code must be the same - Whenever two objects are not the same in terms of
equals()
method, we have no guarantee for theidhashcode()
, i.e. it might be the same, it might be different - when we use
HashMap.put(key, value)
HashMap compares objects by theirequal()
method. If the two keys areequal()
then the newvalue
is overriden - If two kays have the same
hashcode
then collision occurs and Java deals with it - However if two keys are equal then the new value is overriden, BUT it also implies that the
hashCode()
must be the same, so collision must happen, which is a contradiction with the previous?
Can someone please clarify these steps for me?