Junior — Middle
Under what conditions does the size of the internal array (Bucket) in HashMap increase?
sobes.tech AI
Answer from AI
In Java, HashMap increases the size of its internal array (buckets) when a certain fill threshold is reached to maintain the efficiency of insert and search operations.
Main conditions for resizing:
- When the number of elements in the
HashMapexceeds the product of the current array size and the load factor. The default load factor is 0.75. - When adding a new element, if the current load exceeds this threshold, a resize operation occurs — the internal array is increased (usually doubled), and all existing elements are redistributed across the new buckets.
For example, if the array size is 16 and the load factor is 0.75, then upon adding the 13th element, the array size will increase to 32.
This helps maintain a low collision probability and high performance.