Junior — Middle
Are chains in HashMap implemented as singly or doubly linked lists?
sobes.tech AI
Answer from AI
In the implementation of HashMap in Java, collision chains (buckets) were initially implemented as singly linked lists. However, starting from Java 8, if a bucket contains too many elements (usually more than 8), the singly linked list is transformed into a balanced tree (TreeNode), which improves search performance from O(n) to O(log n).
Thus:
- By default — singly linked list.
- When there are many collisions — transformation into a tree.
Two-way linked lists are not used for collision chains in the standard HashMap implementation.