- 先上源码
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
//threshold--阈值,也就是容量*加载因子0.75,比如 16*0.75=12
}
if (key == null)
return putForNullKey(value);
//1、对key取hash
int hash = hash(key);
//2、计算在数组中的下标index
int i = indexFor(hash, table.length);
//3、遍历链表,比较key如果相等就覆盖,用新value替换旧value,并返回旧value
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
//4、没遍历到就会addEntry(),具体看下面代码
addEntry(hash, key, value, i);
return null;
}
- addEntry:put时如果没有遍历到就会执行addEntry,上面几行是扩容,注意createEntry方法
void addEntry(int hash, K key, V value, int bucketIndex) {
if ((size >= threshold) && (null != table[bucketIndex])) {
//1、当size超过临界阈值threshold,并且即将发生哈希冲突时进行扩容,新容量为旧容量的2倍
resize(2 * table.length);
hash = (null != key) ? hash(key) : 0;
//2、扩容后重新计算插入的位置下标
bucketIndex = indexFor(hash, table.length);
}
//3、把元素放入HashMap的对应位置
createEntry(hash, key, value, bucketIndex);
}
//4、创建元素
void createEntry(int hash, K key, V value, int bucketIndex) {
//获取待插入位置元素
Entry<K,V> e = table[bucketIndex];
//这里执行链接操作,使得新插入的元素指向原有元素。
//这保证了新插入的元素总是在链表的头
table[bucketIndex] = new Entry<>(hash, key, value, e);
size++;//元素个数+1
}
- inflateTable
如果是一个空的hashmap就会初始化,这里的tosize其实就是
时这里的容量(比如说我写成15),函数就会调用roundUpTpPowerOf2(),取大于15的那个2^n数(这里也就是16;比如说我填30,那就会取32)。
private void inflateTable(int toSize) {
// Find a power of 2 >= toSize
//capacity一定是2的次幂
int capacity = roundUpToPowerOf2(toSize);
/*
此处为threshold赋值,取capacity*loadFactor和MAXIMUM_CAPACITY+1的最小值,capaticy一定不会
超过MAXIMUM_CAPACITY,除非loadFactor大于1
*/
threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
//分配空间
table = new Entry[capacity];
//选择合适的Hash因子
initHashSeedAsNeeded(capacity);
}
- indexFor:
h&(length-1)保证获取的index一定在数组范围内,举个例子,默认容量16,length-1=15,h=18,转换成二进制计算为
最终计算出的index=2。
static int indexFor(int h, int length) {
return h & (length-1);
}
- resize:扩容操作
//按新的容量扩容Hash表
void resize(int newCapacity) {
//老的数据
Entry[] oldTable = table;
//获取老的容量值
int oldCapacity = oldTable.length;
//老的容量值已经到了最大容量值
if (oldCapacity == MAXIMUM_CAPACITY) {
//修改扩容阀值
threshold = Integer.MAX_VALUE;
return;
}
//新的结构
Entry[] newTable = new Entry[newCapacity];
//将老的表中的数据拷贝到新的结构中
transfer(newTable, initHashSeedAsNeeded(newCapacity));
//修改HashMap的底层数组
table = newTable;
//修改阀值
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
- transfer: 如果数组进行扩容,数组长度发生变化,而存储位置 index = h&(length-1),index也可能会发生变化,需要重新计算index,我们先来看看transfer这个方法:
//将老的表中的数据拷贝到新的结构中
void transfer(Entry[] newTable, boolean rehash) {
//容量
int newCapacity = newTable.length;
//遍历所有链表
for (Entry<K,V> e : table) {
//遍历链表中所有元素
while(null != e) {
Entry<K,V> next = e.next;
//如果是重新Hash,则需要重新计算hash值
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
//定位Hash桶
int i = indexFor(e.hash, newCapacity);
//元素连接到桶中,这里相当于单链表的插入,总是插入在最前面
e.next = newTable[i];
//newTable[i]的值总是最新插入的值
newTable[i] = e;
//继续下一个元素
e = next;
}
}
}
这个方法将老数组中的数据逐个链表地遍历,重新计算后放入新的扩容后的数组中,我们的数组索引位置的计算是通过 对key值的hashcode进行hash扰乱运算后,再通过和 length-1进行位运算得到最终数组索引位置。