微信公众号:I am CR7
若有问题或建议,请在下方留言
最近更新:2018-09-14java
做为哈希表的Map接口实现,其具有如下几个特色:数组
1/**
2 * 默认初始大小,值为16,要求必须为2的幂
3 */
4static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
5
6/**
7 * 最大容量,必须不大于2^30
8 */
9static final int MAXIMUM_CAPACITY = 1 << 30;
10
11/**
12 * 默认加载因子,值为0.75
13 */
14static final float DEFAULT_LOAD_FACTOR = 0.75f;
15
16/**
17 * hash冲突默认采用单链表存储,当单链表节点个数大于8时,会转化为红黑树存储
18 */
19static final int TREEIFY_THRESHOLD = 8;
20
21/**
22 * hash冲突默认采用单链表存储,当单链表节点个数大于8时,会转化为红黑树存储。
23 * 当红黑树中节点少于6时,则转化为单链表存储
24 */
25static final int UNTREEIFY_THRESHOLD = 6;
26
27/**
28 * hash冲突默认采用单链表存储,当单链表节点个数大于8时,会转化为红黑树存储。
29 * 可是有一个前提:要求数组长度大于64,不然不会进行转化
30 */
31static final int MIN_TREEIFY_CAPACITY = 64;
复制代码
注意:HashMap默认采用数组+单链表方式存储元素,当元素出现哈希冲突时,会存储到该位置的单链表中。可是单链表不会一直增长元素,当元素个数超过8个时,会尝试将单链表转化为红黑树存储。可是在转化前,会再判断一次当前数组的长度,只有数组长度大于64才处理。不然,进行扩容操做。此处先提到这,后续会有详细的讲解。安全
问:为什么加载因子默认为0.75?
答:经过源码里的javadoc注释看到,元素在哈希表中分布的桶频率服从参数为0.5的泊松分布,具体能够参考下StackOverflow里的解答:stackoverflow.com/questions/1…微信
1public HashMap() {
2 this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
3}
复制代码
1public HashMap(int initialCapacity) {
2 this(initialCapacity, DEFAULT_LOAD_FACTOR);
3}
复制代码
1public HashMap(int initialCapacity, float loadFactor) {
2 if (initialCapacity < 0)
3 throw new IllegalArgumentException("Illegal initial capacity: " +
4 initialCapacity);
5 if (initialCapacity > MAXIMUM_CAPACITY)
6 initialCapacity = MAXIMUM_CAPACITY;
7 if (loadFactor <= 0 || Float.isNaN(loadFactor))
8 throw new IllegalArgumentException("Illegal load factor: " +
9 loadFactor);
10 this.loadFactor = loadFactor;
11 this.threshold = tableSizeFor(initialCapacity)//经过后面扩容的方法知道,该值就是初始建立数组时的长度
12}
13
14//返回大于等于cap最小的2的幂,如cap为12,结果就是16
15static final int tableSizeFor(int cap) {
16 int n = cap - 1;//为了保证当cap自己是2的幂的状况下,可以返回本来的数,不然返回的是cap的2倍
17 n |= n >>> 1;
18 n |= n >>> 2;
19 n |= n >>> 4;
20 n |= n >>> 8;
21 n |= n >>> 16;
22 return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
23}
复制代码
下面咱们以cap等于8为例:app
问:为什么数组容量必须是2次幂?
答:索引计算公式为i = (n - 1) & hash,若是n为2次幂,那么n-1的低位就全是1,哈希值进行与操做时能够保证低位的值不变,从而保证分布均匀,效果等同于hash%n,可是位运算比取余运算要高效的多。ide
1public HashMap(Map<? extends K, ? extends V> m) {
2 this.loadFactor = DEFAULT_LOAD_FACTOR;
3 putMapEntries(m, false);
4}
5
6final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
7 int s = m.size();
8 if (s > 0) {
9 if (table == null) { // pre-size
10 float ft = ((float)s / loadFactor) + 1.0F;
11 int t = ((ft < (float)MAXIMUM_CAPACITY) ?
12 (int)ft : MAXIMUM_CAPACITY);
13 if (t > threshold)
14 threshold = tableSizeFor(t);
15 }
16 else if (s > threshold)
17 resize();
18 for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
19 K key = e.getKey();
20 V value = e.getValue();
21 putVal(hash(key), key, value, false, evict);
22 }
23 }
24}
复制代码
1public V put(K key, V value) {
2 return putVal(hash(key), key, value, false, true);
3}
4
5//将key的哈希值,进行高16位和低16位异或操做,增长低16位的随机性,下降哈希冲突的可能性
6static final int hash(Object key) {
7 int h;
8 return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
9}
10
11final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
12 boolean evict) {
13 Node<K,V>[] tab; Node<K,V> p; int n, i;
14 //首次table为null,首先经过resize()进行数组初始化
15 if ((tab = table) == null || (n = tab.length) == 0)
16 n = (tab = resize()).length;
17 //利用index=(n-1)&hash的方式,找到索引位置
18 //若是索引位置无元素,则建立Node对象,存入数组该位置中
19 if ((p = tab[i = (n - 1) & hash]) == null)
20 tab[i] = newNode(hash, key, value, null);
21 else { //若是索引位置已有元素,说明hash冲突,存入单链表或者红黑树中
22 Node<K,V> e; K k;
23 //hash值和key值都同样,则进行value值的替代
24 if (p.hash == hash &&
25 ((k = p.key) == key || (key != null && key.equals(k))))
26 e = p;
27 else if (p instanceof TreeNode) //hash值一致,key值不一致,且p为红黑树结构,则往红黑树中添加
28 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
29 else { //hash值一致,key值不一致,且p为单链表结构,则往单链表中添加
30 for (int binCount = 0; ; ++binCount) {
31 if ((e = p.next) == null) {
32 p.next = newNode(hash, key, value, null); //追加到单链表末尾
33 if (binCount >= TREEIFY_THRESHOLD - 1) // //超过树化阈值则进行树化操做
34 treeifyBin(tab, hash);
35 break;
36 }
37 if (e.hash == hash &&
38 ((k = e.key) == key || (key != null && key.equals(k))))
39 break;
40 p = e;
41 }
42 }
43 if (e != null) { // existing mapping for key
44 V oldValue = e.value;
45 if (!onlyIfAbsent || oldValue == null)
46 e.value = value;
47 afterNodeAccess(e);
48 return oldValue;
49 }
50 }
51 ++modCount;
52 if (++size > threshold) //当元素个数大于新增阈值,则经过resize()扩容
53 resize();
54 afterNodeInsertion(evict);
55 return null;
56}
复制代码
问:获取hash值时:为什么在hash方法中加上异或无符号右移16位的操做?
答:此方式是采用"扰乱函数"的解决方案,将key的哈希值,进行高16位和低16位异或操做,增长低16位的随机性,下降哈希冲突的可能性。函数
下面咱们经过一个例子,来看下有无"扰乱函数"的状况下,计算出来索引位置的值:
性能
1final Node<K,V>[] resize() {
2 Node<K,V>[] oldTab = table;
3 int oldCap = (oldTab == null) ? 0 : oldTab.length;
4 int oldThr = threshold;
5 int newCap, newThr = 0;
6 if (oldCap > 0) {//数组不为空
7 if (oldCap >= MAXIMUM_CAPACITY) { //当前长度超过MAXIMUM_CAPACITY,新增阈值为Integer.MAX_VALUE
8 threshold = Integer.MAX_VALUE;
9 return oldTab;
10 }
11 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
12 oldCap >= DEFAULT_INITIAL_CAPACITY) //进行2倍扩容,若是当前长度超过初始16,新增阈值也作2倍扩容
13 newThr = oldThr << 1; // double threshold
14 }
15 else if (oldThr > 0) // 数组为空,指定了新增阈值
16 newCap = oldThr;
17 else { //数组为空,未指定新增阈值,采用默认初始大小和加载因子,新增阈值为16*0.75=12
18 newCap = DEFAULT_INITIAL_CAPACITY;
19 newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
20 }
21 if (newThr == 0) { //按照给定的初始大小计算扩容后的新增阈值
22 float ft = (float)newCap * loadFactor;
23 newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
24 (int)ft : Integer.MAX_VALUE);
25 }
26 threshold = newThr; //扩容后的新增阈值
27 @SuppressWarnings({"rawtypes","unchecked"})
28 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; //扩容后的数组
29 table = newTab;
30 if (oldTab != null) { //将原数组中元素放入扩容后的数组中
31 for (int j = 0; j < oldCap; ++j) {
32 Node<K,V> e;
33 if ((e = oldTab[j]) != null) {
34 oldTab[j] = null;
35 if (e.next == null) //无后继节点,则直接计算在新数组中位置,放入便可
36 newTab[e.hash & (newCap - 1)] = e;
37 else if (e instanceof TreeNode) //为树节点须要拆分
38 ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
39 else { //有后继节点,且为单链表,将原数组中单链表元素进行拆分,一部分在原索引位置,一部分在原索引+原数组长度
40 Node<K,V> loHead = null, loTail = null; //保存在原索引的链表
41 Node<K,V> hiHead = null, hiTail = null; //保存在新索引的链表
42 Node<K,V> next;
43 do {
44 next = e.next;
45 if ((e.hash & oldCap) == 0) { //哈希值和原数组长度进行&操做,为0则在原数组的索引位置,非0则在原数组索引位置+原数组长度的新位置
46 if (loTail == null)
47 loHead = e;
48 else
49 loTail.next = e;
50 loTail = e;
51 }
52 else {
53 if (hiTail == null)
54 hiHead = e;
55 else
56 hiTail.next = e;
57 hiTail = e;
58 }
59 } while ((e = next) != null);
60 if (loTail != null) {
61 loTail.next = null;
62 newTab[j] = loHead;
63 }
64 if (hiTail != null) {
65 hiTail.next = null;
66 newTab[j + oldCap] = hiHead;
67 }
68 }
69 }
70 }
71 }
72 return newTab;
73}
复制代码
状况一:ui
1HashMap<String, Integer> hashMap = new HashMap<>();
复制代码
1int oldCap = (oldTab == null) ? 0 : oldTab.length;
2int oldThr = threshold;
复制代码
1newCap = DEFAULT_INITIAL_CAPACITY;
2newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
3threshold = newThr;
4@SuppressWarnings({"rawtypes","unchecked"})
5 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
6table = newTab;
复制代码
状况二:this
1HashMap<String, Integer> hashMap = new HashMap<>(7);
复制代码
1else if (oldThr > 0) // initial capacity was placed in threshold
2 newCap = oldThr;
复制代码
1if (newThr == 0) {
2 float ft = (float)newCap * loadFactor;
3 newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
4 (int)ft : Integer.MAX_VALUE);
5}
6threshold = newThr;
7@SuppressWarnings({"rawtypes","unchecked"})
8 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
9table = newTab;
复制代码
接着2.2里的状况二,继续添加元素,直到扩容:
1if (oldCap > 0) {
2 if (oldCap >= MAXIMUM_CAPACITY) {
3 threshold = Integer.MAX_VALUE;
4 return oldTab;
5 }
6 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
7 oldCap >= DEFAULT_INITIAL_CAPACITY)
8 newThr = oldThr << 1; // double threshold
9}
复制代码
1if (newThr == 0) {
2 float ft = (float)newCap * loadFactor;
3 newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
4 (int)ft : Integer.MAX_VALUE);
5}
6threshold = newThr;
7@SuppressWarnings({"rawtypes","unchecked"})
8 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
9table = newTab;
10..........省略.......//将原数组元素存入新数组中
复制代码
继续添加元素,直到扩容:
1if (oldCap > 0) {
2 if (oldCap >= MAXIMUM_CAPACITY) {
3 threshold = Integer.MAX_VALUE;
4 return oldTab;
5 }
6 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
7 oldCap >= DEFAULT_INITIAL_CAPACITY)
8 newThr = oldThr << 1; // double threshold
9}
复制代码
1threshold = newThr;
2@SuppressWarnings({"rawtypes","unchecked"})
3 Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
4table = newTab;
5..........省略.......//将原数组元素存入新数组中
复制代码
继续添加元素,扩容到数组长度等于MAXIMUM_CAPACITY:
1if (oldCap > 0) {
2 if (oldCap >= MAXIMUM_CAPACITY) {
3 threshold = Integer.MAX_VALUE;
4 return oldTab;
5 }
6 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
7 oldCap >= DEFAULT_INITIAL_CAPACITY)
8 newThr = oldThr << 1; // double threshold
9}
复制代码
将本来的单链表转化为双向链表,再遍历这个双向链表转化为红黑树:
1final void treeifyBin(Node<K,V>[] tab, int hash) {
2 int n, index; Node<K,V> e;
3 //树形化还有一个要求就是数组长度必须大于等于64,不然继续采用扩容策略
4 if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
5 resize();
6 else if ((e = tab[index = (n - 1) & hash]) != null) {
7 TreeNode<K,V> hd = null, tl = null;//hd指向首节点,tl指向尾节点
8 do {
9 TreeNode<K,V> p = replacementTreeNode(e, null);//将链表节点转化为红黑树节点
10 if (tl == null) // 若是尾节点为空,说明尚未首节点
11 hd = p; // 当前节点做为首节点
12 else { // 尾节点不为空,构造一个双向链表结构,将当前节点追加到双向链表的末尾
13 p.prev = tl; // 当前树节点的前一个节点指向尾节点
14 tl.next = p; // 尾节点的后一个节点指向当前节点
15 }
16 tl = p; // 把当前节点设为尾节点
17 } while ((e = e.next) != null); // 继续遍历单链表
18 //将本来的单链表转化为一个节点类型为TreeNode的双向链表
19 if ((tab[index] = hd) != null) // 把转换后的双向链表,替换数组原来位置上的单向链表
20 hd.treeify(tab); // 将当前双向链表树形化
21 }
22}
复制代码
将双向链表转化为红黑树的具体实现:
1final void treeify(Node<K,V>[] tab) {
2 TreeNode<K,V> root = null; // 定义红黑树的根节点
3 for (TreeNode<K,V> x = this, next; x != null; x = next) { // 从TreeNode双向链表的头节点开始逐个遍历
4 next = (TreeNode<K,V>)x.next; // 头节点的后继节点
5 x.left = x.right = null;
6 if (root == null) {
7 x.parent = null;
8 x.red = false;
9 root = x; // 头节点做为红黑树的根,设置为黑色
10 }
11 else { // 红黑树存在根节点
12 K k = x.key;
13 int h = x.hash;
14 Class<?> kc = null;
15 for (TreeNode<K,V> p = root;;) { // 从根开始遍历整个红黑树
16 int dir, ph;
17 K pk = p.key;
18 if ((ph = p.hash) > h) // 当前红黑树节点p的hash值大于双向链表节点x的哈希值
19 dir = -1;
20 else if (ph < h) // 当前红黑树节点的hash值小于双向链表节点x的哈希值
21 dir = 1;
22 else if ((kc == null &&
23 (kc = comparableClassFor(k)) == null) ||
24 (dir = compareComparables(kc, k, pk)) == 0) // 当前红黑树节点的hash值等于双向链表节点x的哈希值,则若是key值采用比较器一致则比较key值
25 dir = tieBreakOrder(k, pk); //若是key值也一致则比较className和identityHashCode
26
27 TreeNode<K,V> xp = p;
28 if ((p = (dir <= 0) ? p.left : p.right) == null) { // 若是当前红黑树节点p是叶子节点,那么双向链表节点x就找到了插入的位置
29 x.parent = xp;
30 if (dir <= 0) //根据dir的值,插入到p的左孩子或者右孩子
31 xp.left = x;
32 else
33 xp.right = x;
34 root = balanceInsertion(root, x); //红黑树中插入元素,须要进行平衡调整(过程和TreeMap调整逻辑如出一辙)
35 break;
36 }
37 }
38 }
39 }
40 //将TreeNode双向链表转化为红黑树结构以后,因为红黑树是基于根节点进行查找,因此必须将红黑树的根节点做为数组当前位置的元素
41 moveRootToFront(tab, root);
42}
复制代码
将红黑树的根节点移动到数组的索引所在位置上:
1static <K,V> void moveRootToFront(Node<K,V>[] tab, TreeNode<K,V> root) {
2 int n;
3 if (root != null && tab != null && (n = tab.length) > 0) {
4 int index = (n - 1) & root.hash; //找到红黑树根节点在数组中的位置
5 TreeNode<K,V> first = (TreeNode<K,V>)tab[index]; //获取当前数组中该位置的元素
6 if (root != first) { //红黑树根节点不是数组当前位置的元素
7 Node<K,V> rn;
8 tab[index] = root;
9 TreeNode<K,V> rp = root.prev;
10 if ((rn = root.next) != null) //将红黑树根节点先后节点相连
11 ((TreeNode<K,V>)rn).prev = rp;
12 if (rp != null)
13 rp.next = rn;
14 if (first != null) //将数组当前位置的元素,做为红黑树根节点的后继节点
15 first.prev = root;
16 root.next = first;
17 root.prev = null;
18 }
19 assert checkInvariants(root);
20 }
21}
复制代码
1final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,
2 int h, K k, V v) {
3 Class<?> kc = null;
4 boolean searched = false;
5 TreeNode<K,V> root = (parent != null) ? root() : this;
6 for (TreeNode<K,V> p = root;;) {
7 int dir, ph; K pk;
8 if ((ph = p.hash) > h)//进行哈希值的比较
9 dir = -1;
10 else if (ph < h)
11 dir = 1;
12 else if ((pk = p.key) == k || (k != null && k.equals(pk)))
13 return p;
14 else if ((kc == null &&
15 (kc = comparableClassFor(k)) == null) ||
16 (dir = compareComparables(kc, k, pk)) == 0) {//hash值相同,则按照key进行比较
17 if (!searched) {
18 TreeNode<K,V> q, ch;
19 searched = true;
20 if (((ch = p.left) != null &&
21 (q = ch.find(h, k, kc)) != null) ||//去左子树中查找哈希值相同,key相同的节点
22 ((ch = p.right) != null &&
23 (q = ch.find(h, k, kc)) != null))//去右子树中查找哈希值相同,key相同的节点
24 return q;
25 }
26 dir = tieBreakOrder(k, pk);//经过比较k与pk的hashcode
27 }
28
29 TreeNode<K,V> xp = p;
30 if ((p = (dir <= 0) ? p.left : p.right) == null) {//找到红黑树合适的位置插入
31 Node<K,V> xpn = xp.next;
32 TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn);
33 if (dir <= 0) //插入到左节点或者右节点
34 xp.left = x;
35 else
36 xp.right = x;
37 xp.next = x;//插入到双向链表合适的位置
38 x.parent = x.prev = xp;
39 if (xpn != null)
40 ((TreeNode<K,V>)xpn).prev = x;
41 moveRootToFront(tab, balanceInsertion(root, x));//作插入后的平衡调整 将平衡后的红黑树节点做为数组该位置的元素
42 return null;
43 }
44 }
45}
复制代码
当hash冲突时,单链表元素个数超过树化阈值(TREEIFY_THRESHOLD)后,转化为红黑树存储。以后再继续冲突,则就变成往红黑树中插入元素了。关于红黑树插入元素,请看我以前写的文章:TreeMap之元素插入
将红黑树按照扩容后的数组,从新计算索引位置,而且拆分后的红黑树还须要判断个数,从而决定是作去树化操做仍是树化操做:
1final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
2 TreeNode<K,V> b = this;
3 // Relink into lo and hi lists, preserving order
4 TreeNode<K,V> loHead = null, loTail = null; //保存在原索引的红黑树
5 TreeNode<K,V> hiHead = null, hiTail = null; //保存在新索引的红黑树
6 int lc = 0, hc = 0;
7 for (TreeNode<K,V> e = b, next; e != null; e = next) {
8 next = (TreeNode<K,V>)e.next;
9 e.next = null;
10 if ((e.hash & bit) == 0) { //哈希值和原数组长度进行&操做,为0则在原数组的索引位置,非0则在原数组索引位置+原数组长度的新位置
11 if ((e.prev = loTail) == null)
12 loHead = e;
13 else
14 loTail.next = e;
15 loTail = e;
16 ++lc;
17 }
18 else {
19 if ((e.prev = hiTail) == null)
20 hiHead = e;
21 else
22 hiTail.next = e;
23 hiTail = e;
24 ++hc;
25 }
26 }
27
28 if (loHead != null) {
29 if (lc <= UNTREEIFY_THRESHOLD) //当红黑树的节点不大于去树化阈值,则将原索引处的红黑树进行去树化操做
30 tab[index] = loHead.untreeify(map); //红黑树根节点做为原索引处的元素
31 else { //当红黑树的节点大于去树化阈值,则将原索引处的红黑树进行树化操做
32 tab[index] = loHead;
33 if (hiHead != null) // (else is already treeified)
34 loHead.treeify(tab);
35 }
36 }
37 if (hiHead != null) {
38 if (hc <= UNTREEIFY_THRESHOLD) //当红黑树的节点不大于去树化阈值,则将新索引处的红黑树进行去树化操做
39 tab[index + bit] = hiHead.untreeify(map); //红黑树根节点做为新索引处的元素
40 else { //当红黑树的节点大于去树化阈值,则将新索引处的红黑树进行树化操做
41 tab[index + bit] = hiHead;
42 if (loHead != null)
43 hiHead.treeify(tab);
44 }
45 }
46}
复制代码
遍历红黑树,还原成单链表结构:
1final Node<K,V> untreeify(HashMap<K,V> map) {
2 Node<K,V> hd = null, tl = null;
3 for (Node<K,V> q = this; q != null; q = q.next) { //遍历红黑树,依次将TreeNode转化为Node,还原成单链表形式
4 Node<K,V> p = map.replacementNode(q, null);
5 if (tl == null)
6 hd = p;
7 else
8 tl.next = p;
9 tl = p;
10 }
11 return hd;
12}
复制代码
1//插入38个元素,无hash冲突,依次存入索引0~37的位置
2HashMap<Integer, Integer> hashMap = new HashMap<>(64);
3for(int i=0; i<38; i++){
4 hashMap.put(i, i);
5}
6//依次插入6四、12八、18二、25六、320、384,448,索引位置为0,出现hash冲突,往单链表中插入
7for (int i=1; i <= 7; i++) {
8 hashMap.put(64*i, 64*i);
9}
10//插入512,hash冲突,往单链表中插入。此时单链表个数大于TREEIFY_THRESHOLD,将单链表转化为红黑树
11hashMap.put(64*8, 64*8);
12//插入576,hash冲突,往红黑树中插入
13hashMap.put(64*9, 64*9);
14//hash不冲突,保存到数组索引为38的位置,此时总元素个数为48,新增阈值为48,不作处理。
15hashMap.put(38, 38);
16//hash不冲突,保存到数组索引为39的位置,此时总元素个数为49,新增阈值为48,扩容!!!
17hashMap.put(39, 39);
复制代码