1.无参
只初始化了loadFactor
public class HashMap<K,V> extends AbstractMap<K,V>implements Map<K,V>, Cloneable, Serializable {final float loadFactor;public HashMap() {this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted}
}
2.携带初始容量
将initialCapacity经过计算,换成大于等于initialCapacity的最小2的幂次方,然后赋值给threshold;
在put的时候,threshold会被赋值给table的length,按照threshold来创建table
public class HashMap<K,V> extends AbstractMap<K,V>implements Map<K,V>, Cloneable, Serializable {static final int MAXIMUM_CAPACITY = 1 << 30;public HashMap(int initialCapacity) {this(initialCapacity, DEFAULT_LOAD_FACTOR);}public HashMap(int initialCapacity, float loadFactor) {if (initialCapacity < 0){throw new IllegalArgumentException("Illegal initial capacity: " +initialCapacity);}if (initialCapacity > MAXIMUM_CAPACITY){initialCapacity = MAXIMUM_CAPACITY;}if (loadFactor <= 0 || Float.isNaN(loadFactor)){throw new IllegalArgumentException("Illegal load factor: " +loadFactor);}this.loadFactor = loadFactor;this.threshold = tableSizeFor(initialCapacity); //之后这个值会被赋值给table的容量}static final int tableSizeFor(int cap) {int n = cap - 1;n |= n >>> 1;n |= n >>> 2;n |= n >>> 4;n |= n >>> 8;n |= n >>> 16;return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;}
}
3.tableSizeFor为什么cap需要-1
如果一开始cap是2的幂次方,如果不-1,最后的返回结果会是cap*2