Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › Collections Deep Dive — List, Set, Map, Queue

What exactly is the contract between equals() and hashCode(), and what breaks when you violate it?

Level II · JuniorCollections Deep Dive — List, Set, Map, Queue
Short answer

Equal objects must have equal hash codes (the reverse need not hold), and both must be consistent over the object's lifetime.

Explanation

If you override equals() but not hashCode(), two 'equal' objects can land in different buckets, so a HashMap lookup or HashSet contains() fails to find an entry that's logically present. The other classic bug is using a mutable field in equals/hashCode and then mutating a key already in a map — the entry becomes unreachable because it's hashed under its old code.

Common mistakes
  • Overriding equals without hashCode.
  • Using mutable fields in hashCode for keys that change.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Collections Deep Dive — List, Set, Map, Queue.

Practise free

More on Collections Deep Dive — List, Set, Map, Queue