hashmap当前的size>=threshold扩容系数的时候,会将hashmap的容量翻倍。
这就是为什么hashmap的容量只能有2的30次方。因为如果最大值为2的30次方+1.是无法扩容的。int的最大正整数是2的31次方-1所以。容量最大值只能为2的30次方。
- void addEntry(int hash, K key, V value, int bucketIndex) {
- Entry<K,V> e = table[bucketIndex];
- table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
- if (size++ >= threshold)
- resize(2 * table.length);
- }