Looking at the bottom of HashMap today, we found that the maximum capacity of HashMap is as follows:
Maximum capacity (must be a power of 2 and less than 2 30 times, the incoming capacity is replaced by this value)
static final int maximum_capacity = 1 << 30;
When you see the 1<<30, the "<<" a little vague, when the understanding of "<<" after the use of a problem;
The int type is not 4 bytes altogether 32 bits, why not 1<<31?
First introduce the meaning of the numbers and characters on the right side of the equals sign:
1, "<<" is the left-shift operator, 1 means "1" in decimal, 30 means that the decimal number 1 is converted to binary and the left 30 bits are moved. The value is equal to 2 of the power of 30.
2, why is 2 of 30 power?
Take one byte for example: 1<<2 = 4
0000 0001 (Decimal 1)
Moves two bits to the left and then becomes
0000 0100 (Decimal 4)
The visible 1<<30 equals the 30 powers of 2 in decimal.
Back to the question, why is 2 of the power of 30, rather than 2 of the power of 31 times?
First: Java specifies that the static variable of the static final type is of type int, as to why it is not a byte, long, and so on, because of the compromise that takes into account HashMap performance issues!
Because the int type restricts the length of the variable to 4 bytes in a total of 32 bits, it is arguably possible to move the 31-bit 2 power to the left. But in fact, because the highest one in the binary number is the leftmost one is the sign bit, used to indicate the positive and negative points (0 is positive, 1 is negative), so can only move to the left 30 bits, and cannot move to the highest level of the sign bit!
---------------------This article from the love Code Monk's Csdn blog, full-text address please click: 80139620?utm_source=copy
What is the maximum capacity of HashMap for 2 of the 30-time square?