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

What is a hash collision and how does Java's HashMap resolve it?

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

A collision is when two different keys map to the same bucket index.

Explanation

HashMap resolves it with separate chaining: each bucket holds a small list of entries, and get/put walk that list using equals() to find the exact key. If a single bucket's chain grows long (8+ entries) and the table is large enough, HashMap treeifies that bucket into a red-black tree so its worst case is O(log n) instead of O(n). Open addressing is an alternative collision strategy that some other hash tables use.

Common mistakes
  • Saying collisions overwrite entries.
  • Forgetting equals() picks the right key within a bucket.

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