Java Zero to All All questionsPractise free
Interview questions › Core & Modern Java (8 -> 21) › Collections & Equals/HashCode Contracts

What goes wrong if you mutate an object after using it as a HashMap key or HashSet element?

Level II · JuniorCollections & Equals/HashCode Contracts
Short answer

The map places the entry in the bucket determined by the key's hash at insertion time, so mutating a field that participates in hashCode moves the object's logical bucket while it physically stays put.

Explanation

After that, contains and get compute the new hash, look in the wrong bucket, and report the element missing even though it's still in the map, effectively leaking it. The rule is to use immutable keys or at least never mutate key fields while the object is in a hash structure; records make excellent keys precisely because they're immutable.

Common mistakes
  • Mutating a key's hash fields after inserting it.
  • Expecting contains() to still find the moved key.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Collections & Equals/HashCode Contracts.

Practise free

More on Collections & Equals/HashCode Contracts