Why is the initial capacity of hashmap set to 16?

static int indexFor(int h, int length) { return h & (length-1); }

We know that for the table of HashMap, the data distribution needs to be uniform (it is best to have only one element for each item, so that it can be found directly), neither too tight nor too loose, too tight will lead to slow query speed, too loose will waste space. After calculating the hash value, how can we ensure that the distribution of table elements is uniform? We will think of modulo, but due to the large consumption of modulo, HashMap is handled in this way: call the indexFor method.

There is an indexFor method in the HashMap source code, which returns the hashcode of the key and the initial capacity -1 to do the AND operation. First, if length is an integer power of 2, h&(length-1) is equivalent to taking the modulo of length, which ensures the uniformity of the hash and also improves the efficiency; secondly, if length is an integer power of 2, is an even number. In this way, length-1 is an odd number, and the last bit of the odd number is 1, which ensures that the last bit of h&(length-1) is 0, or 1 (depending on the value of h), that is, the result after AND May be even or odd. In this way, the uniformity of the hash can be guaranteed,

And if length is odd, it is obvious that length-1 is even, and its last digit is 0, so the last digit of h&(length-1) must be 0, that is, it can only be an even number, so that any hash value is only will be hashed to the even subscript position of the array, which wastes nearly half of the space. Therefore, length is taken to the power of 2 in order to make the probability of collision of different hash values ​​smaller, so that elements can be evenly hashed in the hash table.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324413232&siteId=291194637