問(wèn)題描述
您能否解釋一下來(lái)自 HashMap 構(gòu)造函數(shù) 特別是這一行
Can you please explain this code snippet from HashMap constructor specifically the line
容量<<= 1:
capacity <<= 1:
// Find a power of 2 >= initialCapacity
198 int capacity = 1;
199 while (capacity < initialCapacity)
200 capacity <<= 1;
推薦答案
相當(dāng)于capacity = capacity <<<1;
.
該操作將容量的位向左移動(dòng)一位,相當(dāng)于乘以 2.
It is equivalent to capacity = capacity << 1;
.
That operation shifts capacity's bits one position to the left, which is equivalent to multiplying by 2.
您發(fā)布的特定代碼找到大于 initialCapacity
的 2 的最小冪.
The specific code you posted finds the smallest power of 2 which is larger than initialCapacity
.
所以如果 initialCapacity
為 27,例如 capacity
在循環(huán)后將是 32 (2^5).
So if initialCapacity
is 27, for example, capacity
will be 32 (2^5) after the loop.
這篇關(guān)于<<= 運(yùn)算符在 Java 中是什么意思?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!