e2和cpa都是什么呢?不知道捏

一、简单介绍HashMap的底层实现

HashMap总体是数组+链表的存储结构

基于hashCode,HashMap有着十分出色的查找能力,可以快速寻找内容,但是存储的数据不能重复,也不能排序或者使用index进行遍历

二、HashMap的存储结构

HashMap是基于数组+链表存储的数据结构,在jdk1.8之后,当数组长度大于64且链表长度大于8时,链表会被自动转换为红黑树

HashMap每一个数组都由一个个叫做node的小单元构成,数组初始长度被设置为16

1
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

node元素在链表和红黑树中有不同的结构,红黑树中的TreeNode继承于链表中的Node

1.链表中的源码如下:

1
2
3
4
5
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
  • final int hash为key的hash值,通过调用hashCode()方法并进行一系列计算得到
  • final K key为key
  • V value为key对应的value
  • Node<K,V> next记录着下一个节点的地址值,以此来实现链表

2.红黑树中的源码如下:

1
2
3
4
5
6
7
8
9
static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
TreeNode<K,V> parent; // red-black tree links
TreeNode<K,V> left;
TreeNode<K,V> right;
TreeNode<K,V> prev; // needed to unlink next upon deletion
boolean red;
TreeNode(int hash, K key, V val, Node<K,V> next) {
super(hash, key, val, next);
}

由于继承了链表中的Node,TreeNode也一样拥有以下内容

  • final int hash为key的hash值
  • final K key为key
  • V value为key对应的value
  • Node<K,V> next记录着下一个节点的地址值

同时TreeNode还有以下内容,用来实现红黑树

  • TreeNode<K,V> parent记录父节点的地址值
  • TreeNode<K,V> left记录左子节点的地址值
  • TreeNode<K,V> right记录右子节点的地址值
  • TreeNode<K,V> prev记录上一个节点的地址值
  • boolean red记录节点的颜色,默认为红色

三、HashMap的put操作过程

我们假定运行如下代码

1
2
3
4
5
6
HashMap<String,Integer> hm = new HashMap<>();
hm.put("aaa" , 111);
hm.put("bbb" , 222);
hm.put("ccc" , 333);
hm.put("ddd" , 444);
hm.put("eee" , 555);

进行put操作时,至少要考虑三种情况:

  1. 数组位置为null
  2. 数组位置不为null,key出现了重复,进行元素覆盖
  3. 数组位置不为null,key不重复,挂在下面形成链表,进而形成红黑树

put操作源码如下,输入为key和value

1
2
3
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}

返回值为被覆盖元素的值,如果没有进行覆盖,就返回null

putVal(hash(key), key, value, false, true)此方法中调用了hash()方法,方法如下

1
2
3
4
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

此方法即利用key计算出hash值,再把hash值进行另外的一些处理,并进行返回


1.数组位置为null

putVal源码如下:

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
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
//定义一个局部变量,用来记录hash表中数组的地址值
Node<K,V>[] tab;
//临时的第三方变量,用来记录键值对对象的地址值
Node<K,V> p;
//n为当前数组的长度,i为索引
int n, i;
//把hash表中数组的地址值赋值给tab,并将tab的长度赋值给n
if ((tab = table) == null || (n = tab.length) == 0)
//调用resize方法
//如果当前是第一次添加数据,底层会创建一个默认长度为16,加载因子为0.75的数组
//如果不是第一次添加数据,会看底层的元素是否达到了扩容的条件
//如果达到了扩容条件,底层会将数组扩容为原来的两倍,并将数据全部转移过去
//将调用resize方法后的数组长度赋值给n
n = (tab = resize()).length;
//拿着数组的长度和key的hash值进行计算,计算出当前键值对应该存入的位置
if ((p = tab[i = (n - 1) & hash]) == null)
//如果当前位置为空,就创建一个node对,并进行存入
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))))
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) {
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;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//threshold:记录着数组的长度 * 0.75,hash表的扩容时机
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}

其中final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict)方法块中,参数hash为刚刚求出的hash值,key为键,value为值,onlyIfAbsent为如果键发生重复,是否保留


2.数组位置不为null,key出现了重复,进行元素覆盖

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
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
//定义一个局部变量,用来记录hash表中数组的地址值
Node<K,V>[] tab;
//临时的第三方变量,用来记录键值对对象的地址值
Node<K,V> p;
//n为当前数组的长度,i为索引
int n, i;
//把hash表中数组的地址值赋值给tab,并将tab的长度赋值给n
if ((tab = table) == null || (n = tab.length) == 0)
//调用resize方法
//如果当前是第一次添加数据,底层会创建一个默认长度为16,加载因子为0.75的数组
//如果不是第一次添加数据,会看底层的元素是否达到了扩容的条件
//如果达到了扩容条件,底层会将数组扩容为原来的两倍,并将数据全部转移过去
//将调用resize方法后的数组长度赋值给n
n = (tab = resize()).length;
//拿着数组的长度和key的hash值进行计算,计算出当前键值对应该存入的位置
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e;
K k;
//p.hash == hash左边为数组中键值对的hash值
//右边为要添加键值对的hash值,如果key不一样,则返回false
//如果key一样,返回true
if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
//for下面是寻找链表应该存入的位置
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;
}
//如果发生了hash碰撞,就会结束寻找
if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))
break;
//将下一个节点的地址值交给p
p = e;
}
}
//进行覆盖
if (e != null) { // existing mapping for key
//记录旧的value
V oldValue = e.value;
//进行覆盖
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
//返回旧的value
return oldValue;
}
}
++modCount;
//threshold:记录着数组的长度 * 0.75,hash表的扩容时机
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}

3.数组位置不为null,key不重复,挂在下面形成链表,进而形成红黑树

putVal源码如下:

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
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
//定义一个局部变量,用来记录hash表中数组的地址值
Node<K,V>[] tab;
//临时的第三方变量,用来记录键值对对象的地址值
Node<K,V> p;
//n为当前数组的长度,i为索引
int n, i;
//把hash表中数组的地址值赋值给tab,并将tab的长度赋值给n
if ((tab = table) == null || (n = tab.length) == 0)
//调用resize方法
//如果当前是第一次添加数据,底层会创建一个默认长度为16,加载因子为0.75的数组
//如果不是第一次添加数据,会看底层的元素是否达到了扩容的条件
//如果达到了扩容条件,底层会将数组扩容为原来的两倍,并将数据全部转移过去
//将调用resize方法后的数组长度赋值给n
n = (tab = resize()).length;
//拿着数组的长度和key的hash值进行计算,计算出当前键值对应该存入的位置
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))))
e = p;
//判断数组中取出来的键值对是不是红黑树中的节点
//如果是,则调用putTreeVal方法,把当前节点按照规则添加到红黑树中
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) {
//如果没有,就将创建一个新的节点,并挂载在下方形成链表
p.next = newNode(hash, key, value, null);
//判断当前链表长度是否超过8,如果超过8,就会调用treeifyBin方法
//treeifyBin底层还会继续判断数组长度是不是大于6 4
//如果同时满足,就会把链表转化为红黑树
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;
}
}
//如果e为null,表示当前不需要覆盖任何元素
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//threshold:记录着数组的长度 * 0.75,hash表的扩容时机
if (++size > threshold)
resize();
afterNodeInsertion(evict);

//表示当前没有覆盖任何元素,返回null
return null;
}

if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k))))此语句中包含三个命题:

p.hash == hash左边为数组中键值对的hash值,右边为要添加键值对的hash值,如果key不一样,则返回false