HashMap原理分析(一)

HashMap的底层原理(一)

组成结构

数据 + 链表的形式+红黑树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// node 数组
transient Node<K,V>[] table;
// Node结构
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;
}
}

其他参数

负载因子

1
2
3
4
/**
* The load factor used when none specified in constructor.
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;

链表最大长度:

1
static final int TREEIFY_THRESHOLD = 8;

链表转换为红黑树的重要参数

1
2
// 链表长度 > 8 并且数组长度小于64那么进行扩容,否则转为红黑树
static final int MIN_TREEIFY_CAPACITY = 64;

链表长度的界限

1
static final int TREEIFY_THRESHOLD = 8;

使用

1
2
3
4
5
6
// 定义HashMap,初始大小可不写
HashMap<String, String> map = new HashMap<>(5);
// 存放值
map.put("test","name2");
// 取值
String test = map.get("test");

原理解析

简括流程

  • 计算key的hash,高16位于低16位进行异或运算

    1
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
  • 判断数组是否为空,为空时初始化。采用初次扩容。初始化的数组大小为16,扩容界限为16x0.75=12

    1
    2
    3
    Node<K,V>[] tab; Node<K,V> p; int n, i;
    if ((tab = table) == null || (n = tab.length) == 0)
    n = (tab = resize()).length;
  • 使用hash&(n-1)获取key存放的下标位置,初始化Node,存放进Node [i]

    1
    2
    if ((p = tab[i = (n - 1) & hash]) == null)
    tab[i] = newNode(hash, key, value, null);
  • key的hash值与初始化的第一个Node的hash一致,并且key的值也一致,直接覆盖

    1
    2
    3
    4
    Node<K,V> e; K k;
    if (p.hash == hash &&
    ((k = p.key) == key || (key != null && key.equals(k))))
    e = p;
  • 如果是红黑树,直接存放进红黑树内

    1
    e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
  • 遍历Node链表,直到next == null,创建新的Node节点赋值给next。如果链表大小大于8个,则进行扩容或转红黑树操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    for (int binCount = 0; ; ++binCount) {
    if ((e = p.next) == null) {
    p.next = newNode(hash, key, value, null);
    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 = e;
    }
  • 判断map大小是否大于扩容大小,是的话进行扩容

    1
    2
    3
    4
    // size:hashMao大小
    // threshold:threshold = newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
    if (++size > threshold)
    resize();
  • 覆盖旧值操作

    1
    2
    3
    4
    5
    6
    7
    if (e != null) { // existing mapping for key
    V oldValue = e.value;
    if (!onlyIfAbsent || oldValue == null)
    e.value = value;
    afterNodeAccess(e);
    return oldValue;
    }

详细代码

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// 扩容操作 
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) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
// 成倍的扩容,16 = 010000 << 1 --> 100000 = 32
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
// 初始化时的扩容操作
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 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;
/**
* oldCap 16: 10000
* 假设e.hash = 9
* 第一次存放的位置:01001 & 01111 = 01001 = 9
* 为了保证扩容后用同样的操作取下标结果不会改变,也就是通过(n - 1) & hash还会取到e
* 扩容后:newCap = 32 = 100000
* 001001 & 011111 = 01001 = 9
* 所以对e这个数据不改变下标位置
* 01001 &
* 10000 = 0
**/
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
/**
* oldCap 16: 10000
* 假设e.hash = 20
* 第一次存放的位置:10100 & 01111 = 00100 = 4
* 为了保证扩容后用同样的操作取下标结果不会改变,也就是通过(n - 1) & hash还会取到e
* 扩容后:newCap = 32 = 100000
* 010100 & 011111 = 010100 = 20
* 所以对e这个数据要改变下标位置为20
* 10100 &
* 10000 = 10000 != 0
* 所以要对该数据进行迁移,迁移坐标为:4 + 16(扩容的大小) = 20
**/
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;
}
}
}
}
}
return newTab;
}