how and when Rehashing is done in HashMap

前端 未结 2 948
无人及你
无人及你 2021-02-10 19:34

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

2条回答
  •  别跟我提以往
    2021-02-10 20:09

    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.

提交回复
热议问题