Skip to main content
InterviewHubPRO
Section Overview
Q7

Question 7 of 50

Part 1: Core Language, Memory Model & Collections (1 - 10)

14%
Java Core & JVM (50 Qs)•Part 1: Core Language, Memory Model & Collections (1 - 10)

How does `HashMap` work internally in Java 8+?

senior LeveljavaCore Language

In Simple Words: HashMap is an array of buckets (Node<K,V>[]).

  1. Put: Computes hash(key). Uses (n - 1) & hash to find bucket index. If empty, inserts node.
  2. Collision: If bucket is occupied, checks equals(). If not equal, appends to linked list.
  3. Treeification (Java 8+): If a bucket's linked list length exceeds TREEIFY_THRESHOLD (8) and array capacity is at least 64, the linked list converts to a Red-Black Tree (TreeNode<K,V>), reducing worst-case lookup from O(n) to O(log n).
  4. Resize: When size exceeds capacity * loadFactor (default 0.75), capacity doubles (e.g., 16 -> 32).