JDK1.8版本HashMap 方法解析
put方法详解:
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
HashMap.Node<K,V>[] tab; HashMap.Node<K,V> p; int n, i;
//如果存储元素的table为空,则进行一些必要字段的初始化
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;//容量为16
//如果根据hash值获取的节点为空,则新建一个节点
//p指的是根据hash值算出来的数组中的元素
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
HashMap.Node<K,V> e; K k;
//如果插入的节点的p的hash值与插入节点的hash值相等的话,把p赋值给e
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//如果数组中的元素p是树结构的话,则进行红黑树插入操作
else if (p instanceof HashMap.TreeNode)
e = ((HashMap.TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
//否则
for (int binCount = 0; ; ++binCount) {
//代表不是树的情况下,也就是链表只有头元素,没有下一个元素,直接新建一个节点
if ((e = p.next) == null) {
//这时候就是链表结构了,要把待插入元素挂在链尾
p.next = newNode(hash, key, value, null);
//连边长度大于8转为红黑树
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
//更新元素p
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//size太大,达到了capacity的0.75,需要扩容
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
————————————————————————————————————————————————————
resize扩容方法详解:
//初始化或者扩容之后元素调整
final Node<K,V>[] resize() {
// 获取旧元素数组的各种信息
Node<K,V>[] oldTab = table;
// 长度
int oldCap = (oldTab == null) ? 0 : oldTab.length;
// 扩容的临界值
int oldThr = threshold;
// 定义新数组的长度及扩容的临界值
int newCap, newThr = 0;
if (oldCap > 0) { // 如果原table不为空
// 如果数组长度达到最大值,则修改临界值为Integer.MAX_VALUE
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
// 下面就是扩容操作(2倍)
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
// threshold也变为二倍
newThr = oldThr << 1;
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // threshold为0,则使用默认值
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) { // 如果临界值还为0,则设置临界值
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr; // 更新填充因子
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) { // 调整数组大小之后,需要调整红黑树或者链表的指向
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode) // 红黑树调整
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
// 链表调整
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
JDK1.7的HashMap的put方法:
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
//传入了初始的threshold,它在构造方法中进行了第一次赋值
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
//对key进行hash值计算
int hash = hash(key);
//获取到元素在数组中的下标
int i = indexFor(hash, table.length);
////先将数组位置的值赋给新的Entry对象e,如果这个对象e是空的,则跳出for循环,循环方式,e指向下一个e
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
////如果hash值相等,并且key值一样,就开始覆盖
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
//LinkedHashMap实现了该方法
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
void addEntry(int hash, K key, V value, int bucketIndex) {
//如果size大于阈值 并且 数组下标位置不为空
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length);
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
createEntry(hash, key, value, bucketIndex);
}
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
//后面这个new Entry执行链接操作,使得新插入的元素指向原有元素。
//并保证了新插入的元素总是在链表的头,即新插入的元素总是在数组位置上
size++;//元素个数+1
}