Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What is the principle of hashmap extension?
What is the principle of hashmap extension?
The principle of hashmap extension is that HashMap's method is to replace the original array with a new array. Recalculate all the data of the original array, insert it into the new array, and then point to the new array. If the array has reached the maximum before expansion, the threshold will be set directly to the maximum plastic return.

Characteristics of hashmap capacity expansion

The larger the loading factor, the higher the space utilization rate, and the more elements filled before expansion, the faster the put operation, but the linked list is easy to be too long, the higher the hash collision probability, the slower the get operation, the smaller the loading factor, the faster the get operation and the lower the hash collision probability, but the space utilization rate is low, and too many put elements will lead to frequent expansion and affect the performance.

When we use HashMap, if we know the approximate number of elements to be operated in advance, we'd better give an initialization value. First of all, we should try to avoid expansion. Secondly, we should set some values according to business scenarios and important parameters to improve the efficiency of use. HashMap doubles in size every time.