Module: | Arrays, Strings & Exception Handling
Q61: Consider the following statements regarding the internal architecture of HashMap:
1. HashMap structurally permits the insertion of exactly one null key and a mathematically unlimited number of null values.
2. When two entirely distinct keys generate the exact same hash code (a hash collision), HashMap immediately overwrites the older key-value pair to prevent data corruption.
3. Java 8 significantly optimized HashMap performance by automatically converting its internal collision linked lists into balanced binary trees when the bucket collision count reaches a specific threshold.
Which of the above statements is/are correct?
2. When two entirely distinct keys generate the exact same hash code (a hash collision), HashMap immediately overwrites the older key-value pair to prevent data corruption.
3. Java 8 significantly optimized HashMap performance by automatically converting its internal collision linked lists into balanced binary trees when the bucket collision count reaches a specific threshold.
Which of the above statements is/are correct?
✅ Correct Answer: B
🎯 Quick Answer:
The correct combination is 1 and 3. Statement 2 is incorrect because HashMap does not overwrite data during a hash collision. Instead, it securely stores both key-value pairs in the exact same memory bucket by chaining them together in a linked list structure. Overwriting only occurs if the keys themselves evaluate as semantically identical via the .equals() method.Structural Breakdown: The internal array defaults to 16 buckets.
The index is calculated via (hash & (capacity - 1)). If a collision occurs, elements form a Node linked list.
Historical/Related Context: The Java 8 update (Statement 3 context) known as JEP 180 was a massive architectural shift to prevent Denial of Service (DoS) attacks.
Malicious users used to send thousands of specifically crafted web requests that intentionally generated identical hash codes, forcing the server's HashMap to build a massive, slow linked list, downgrading lookup performance from O(1) to a crippling O(n). Causal Reasoning: Converting the linked list into a balanced tree when the threshold (TREEIFY_THRESHOLD = 8) is breached mathematically guarantees that even under a severe collision attack, the absolute worst-case search time complexity drops from a disastrous O(n) to a highly manageable O(log n).