Home > Back-end >  Why Java HashMap array length is 2 n times the power?
Why Java HashMap array length is 2 n times the power?

Time:01-25

See a lot of the answers on the Internet, the main said is 2 ^ n - 1 is an odd number, can guarantee the hash value and its & amp; Operation when the parity of the structure is determined by the hash value,

Why not because 2 ^ n - 1 was a each binary number is 1, and any number and operation results are intercepting low n - one of the number, such ability is equivalent to the hash % (length is n) length of operation, in addition to select 2 ^ n as the length of the hash array, it has a benefit, because when capacity is to 2 times, only need to shift to the left a bit operations can get new array length, also have a little acceleration,

Do you think???????
  • Related