HashMap的使用

Node和初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;

Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
//hashCode通过key于value计算得到
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
}
  • 哈希桶的大小,默认为16,如果自定义,需要为2的n次幂,这样可以使用与运算高效的代替模运算。
  • 负载因子,负载因子*数组长度等于容量阈值。
  • 阈值,当前HashMap中元素元素超过阈值,需要扩容,防止各种操作hash冲突增多,效率变低。

    hash和寻址

    1
    2
    3
    4
    5
    static final int hash(Object key) {
    int h;
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
    index = hash&(tab.length-1);
    hash值通过key的hashCode异或本身高位得到的,这样可以让高位参与运算,否则hash值异或(tab.length-1)将不能利用hash值的高位,导致hash冲突变多。

put过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null) //判断数组位置是否已经插入元素,如果没有就插入数组中
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k)))) //通过hash和key确定,是否key相同,是的话覆盖value即可
e = p;
else if (p instanceof TreeNode) //判断是不是转为了红黑树,是的话将此节点插入树中
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else { //循环遍历链表
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) { //到达尾部,没有找到key相同的节点,插入尾部
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//找到key相同的节点,直接返回,执行1

if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
//1,在key相同的节点,修改value
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//size大于阈值,扩容
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}

get过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k)))) //首先通过hash值在哈希表数组中寻找,如果hash和key都符合,则表示找到,可以返回
return first;
if ((e = first.next) != null) { //出现hash冲突,
if (first instanceof TreeNode)//如果子节点是树节点,在红黑树中寻找
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do { //循环遍历链表,在链表中查找
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}

扩容

在容量超过阈值以后,会进行扩容,数组容量是当前数组的两倍。扩容后会为当前HashMap中每一个桶中的元素重新寻址,新的下标是e.hash&(newCapcity-1)。如果新的hash桶中已有元素,则类似插入元素的方式将当前元素插入hash表。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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; //会形成一个链表,link_1;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e; //也会形成一个链表,link_2;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead; //存link_1在新数组低位
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead; //存link_2在数组高位
}
}
文章作者: hohnor
文章链接: http://www.zhulk3.cn/2022/02/10/HashMap的使用/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 甜茶不贵