What is the reason why the initial capacity of HashMap is set to 16?
What is the reason why the initial capacity of HashMap is set to 16? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
The value of length is the integer power of 2, and h & (length-1) is equivalent to taking the module of length. This improves the efficiency and makes the data distribution more uniform.
Why is it more uniform? If the value of length is even and length-1 is odd, the last binary bit of h & (length-1) is 1, which ensures that the last binary digit of h & (binary-1) may be 1 or 0. If length is odd, half of the space will be wasted.
The answer to the question about why the initial capacity of HashMap is set to 16 is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.