Because the Map still uses x
to test for equality.
Here is what happens:
- you insert in the map using
x
as a key, the hashCode at this time is #x. Great. - you change some values on
x
, #x is now gone, the new hashCode is #x'. - you try to look up the value associated to
x
in the map. The map gets the hashCode: #x'. It does not exist in the map (since at the time of insertion it was #x). - you create
z
with the same valuesx
originally had. - you look up the value associated to
z
. The map found a value for the hashCode ofz
(since it's #x), but then callsequals
onz
andx
(the same instance you used to insert the value during the first step). And you getfalse
since you movedx
!
The map keeps a reference to the instance of the key, and use it to test equals
when you get
, but it never re-computes the hashCode.