Java HashMap collision handling: LinkedList to Red-Black Tree

Before Java 8 — HashMap used an array of LinkedLists for collision handling. Worst case lookup was O(n) — if many keys hashed to the same bucket, you'd traverse the entire chain. Java 8+ — Still starts with LinkedLists, but when a bucket has ≥ 8 nodes, it automatically converts to a Red-Black Tree (TreeNode). This drops worst-case lookup to O(log n). It reverts back to a list if nodes fall to ≤ 6. The critical thresholds to remember for interviews: TREEIFY_THRESHOLD = 8 → list becomes tree UNTREEIFY_THRESHOLD = 6 → tree reverts to list MIN_TREEIFY_CAPACITY = 64 → table must also have ≥ 64 buckets before treeifying #java

  • No alternative text description for this image

This is a really crisp explanation of HashMap internals

See more comments

To view or add a comment, sign in

Explore content categories