static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; //默认初始容量:初始桶数。
static final int MAXIMUM_CAPACITY = 1 << 30;//最大容量:最大桶数。
static final float DEFAULT_LOAD_FACTOR = 0.75f;//负载因子:当箱子数达到这个值时,开始扩容。
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; //设置负载因子为默认值 0.75
}
初始化容量为0,负载因子为0.75
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
Node[] tab; Node p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
...
}
当第一次put的时候,桶数组table为null,开始resize扩容。
final Node[] resize() {
Node[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
...
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
...
threshold = newThr;
...
}
oldCap = 0; oldThr = 0; 进入else分支,执行完之后newCap = 16;newThr = 12;第一次扩容后桶容量为16,阈值为12。
public HashMap(int initialCapacity, float loadFactor) {
//桶容量不能小于0
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
//桶容量不能大于最大桶容量
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
//判断加载因子是否符合规范 大于0的浮点数
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
this.threshold = tableSizeFor(initialCapacity);
}
此构造函数就是给定初始化桶容量和加载因子,通过tableSizeFor方法计算阈值。
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;
}
获取第一个比入参cap大的2的幂指数。例如输入3返回4,输入10返回16。当第一次put的时候,桶数组table仍然为null,开始resize扩容。
final Node[] resize() {
Node[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;//为0
int oldThr = threshold;//经过tableSizeFor方法threshold最小为1
int newCap, newThr = 0;
...
else if (oldThr > 0) {
newCap = oldThr;
}
...
if (newThr == 0) {//根据加载因子计算最新阈值
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
...
}
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
就是用默认加载因子调用HashMap(int initialCapacity, float loadFactor)
public HashMap(Map extends K, ? extends V> m) {
this.loadFactor = DEFAULT_LOAD_FACTOR;
putMapEntries(m, false);
}
给定默认构造因子,将原有map放入到HashMap中,调用putMapEntries方法
final void putMapEntries(Map extends K, ? extends V> m, boolean evict) {
int s = m.size();
if (s > 0) {
//如果桶数组为空,根据m的长度来初始化阈值
if (table == null) { // pre-size
float ft = ((float)s / loadFactor) + 1.0F;
int t = ((ft < (float)MAXIMUM_CAPACITY) ?
(int)ft : MAXIMUM_CAPACITY);
if (t > threshold)
threshold = tableSizeFor(t);
}
//m长度超过阈值,进行扩容
else if (s > threshold)
resize();
//将m中元素放入HashMap中
for (Map.Entry extends K, ? extends V> e : m.entrySet()) {
K key = e.getKey();
V value = e.getValue();
putVal(hash(key), key, value, false, evict);
}
}
}
final Node[] resize() {
Node[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
...
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
...
}
桶数量未到16或者桶数量翻倍后大于极值,桶数量翻倍,然后还要走下面代码
final Node[] resize() {
...
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
...
}
根据最新桶数量和加载因子计算阈值,如果阈值或最新桶数量大于极值,则阈值为最大Integer值。
final Node[] resize() {
Node[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
...
}
...
}
阈值为最大Integer值。