i have some confusion about Hashing and Rehashing. below is my understanding please correct me if i am wrong.
as per picture, bucket is actually the array of En
Rehashing increases the number of available buckets as a function of the number of entries currently stored in the HashMap
.
It occurs when the HashMap
implementation determines the number of buckets should be increased in order to maintain the expected O(1)
lookup and insertion performance.
You are correct regarding the .75 default load factor and how it will cause the HashMap
to be rehashed when the 13th entry is added.
However, it is not correct that default capacity of HashMap 16 means it can store 16 element in it
. Any bucket can store multiple entries. However, in order to preserve the desired performance, the average number of entries per bucket should be small. That's the reason we have the load factor, and the reason we should use a proper hashCode()
that spreads the keys as evenly as possible across the buckets.