如需转载,请根据 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 许可,附上本文作者及链接。
本文作者: 执笔成念
作者昵称: zbcn
本文链接: https://1363653611.github.io/zbcn.github.io/2020/10/06/JCF_01HashMap/
hashMap 的前世今生
简介
hashMap 依据 hashCode 值存储数据。如果通过hashcode 可以直接定位到数据的值,访问的复杂度则为常数,但遍历的顺序则是无序的。
HashMap最多只允许一个key为null,允许多个key的value值为null。
HashMap非线程安全,即任一时刻可以有多个线程同时写HashMap,可能会导致数据的不一致。jdk 1.7 之前的版本有可能在多线程下导致死锁。
如果需要满足线程安全,可以使用ConcurrentHashMap。
hashMap 由数组+ 链表组成。数组是HashMap的主体,链表是为了结局Hash冲突而存在的(拉链法)。
jdk1.8之后
在jdk1.8 之后,在解决hash冲突时,做了调整,当拉链长度大于阈值8 时,将链表调整为红黑树。以减少搜索时间。
将链表转换为红黑树之前会做判断,如果当前的数组长度小于64,那么会优先选择数组扩容,而不是转换为红黑树。源码中的方法为treeifyBin
底层数据结构概述
hash表
散列表是由我们的数组和链表组成的,集成了两种数据结构的优点,我们先简单介绍一下这两种数据结构:
哈希表为每个对象计算出一个整数,称为哈希码。根据这些计算出来的整数(哈希码)保存在对应的位置上!如果遇到了哈希冲突,也就是同一个坑遇到了被占用的情况下,那么我们就会以链表的形式添加在后面。
数组
数组的存储区间,占用内存严重,故空间复杂度较大,但是查找速度很快,为常数级O(1)。(二分查找法更快)
数组的特点: 查找速度快,插入和删除速率低(数据拷贝,位移)
链表
存储区间离散,占用内存比较宽松,故空间复杂度很小。但查找时间复杂度表大,需要循环整个链表O(n),
链表特点:查找速度慢,插入和删除速率高。
红黑树
Jdk1.8 之后,当链表长度大于阈值(默认为8)时,将链表转化为红黑树,以减少搜索时间。
关于红黑树:https://www.jianshu.com/p/e136ec79235c
底层数据结构分析
jdk 1.8 之前
JDK1.8 之前 HashMap 底层是 数组和链表 结合在一起使用也就是 链表散列。HashMap 通过 key 的 hashCode 经过扰动函数处理过后得到 hash 值,然后通过 (n - 1) & hash
判断当前元素存放的位置(这里的 n 指的是数组的长度),如果当前位置存在元素的话,就判断该元素与要存入的元素的 hash 值以及 key 是否相同,如果相同的话,直接覆盖,不相同就通过拉链法解决冲突。
扰动函数:就是 HashMap 的 hash 方法。使用 hash 方法也就是扰动函数是为了防止一些实现比较差的 hashCode() 方法 换句话说使用扰动函数之后可以减少碰撞。
hash方法
- jdk1.7 的HashMap 的hash 方法源码
1 | static int hash(int h) { |
2 | // This function ensures that hashCodes that differ only by |
3 | // constant multiples at each bit position have a bounded |
4 | // number of collisions (approximately 8 at default load factor). |
5 | |
6 | h ^= (h >>> 20) ^ (h >>> 12); |
7 | return h ^ (h >>> 7) ^ (h >>> 4); |
8 | } |
- jdk1.8 的HashMap的hash方法源码
1 | static final int hash(Object key) { |
2 | int h; |
3 | // key.hashCode():返回散列值也就是hashcode |
4 | // ^ :按位异或 |
5 | // >>>:无符号右移,忽略符号位,空位都以0补齐 |
6 | // h = key.hashCode() 为第一步 取hashCode值 |
7 | // h ^ (h >>> 16) 为第二步 高位参与运算 |
8 | // 如果key为null,hash值为0,否则,为key的hashCode ^ 它的hashCode右移16位 |
9 | return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); |
10 | } |
Jdk1.8 的性能比jdk1.7的性能稍微好点,因为jdk17 扰动了4次,而jdk18 只扰动了1次。
拉链法
将链表和数组相结合。也就是说创建一个链表数组,数组中每一格就是一个链表。若遇到哈希冲突,则将冲突的值加到链表中即可。
图如:hash表中实例。
jdk 1.8之后
相比于之前的版本,jdk1.8在解决哈希冲突时有了较大的变化,当链表长度大于阈值(默认为8)时,将链表转化为红黑树,以减少搜索时间。
源码
类的属性
1 | public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable { |
2 | // 序列号 |
3 | private static final long serialVersionUID = 362498820763181265L; |
4 | // 默认的初始容量是16 |
5 | static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; |
6 | // 最大容量 |
7 | static final int MAXIMUM_CAPACITY = 1 << 30; |
8 | // 默认的填充因子 |
9 | static final float DEFAULT_LOAD_FACTOR = 0.75f; |
10 | // 当桶(bucket)上的结点数大于这个值时会转成红黑树 |
11 | static final int TREEIFY_THRESHOLD = 8; |
12 | // 当桶(bucket)上的结点数小于这个值时树转链表 |
13 | static final int UNTREEIFY_THRESHOLD = 6; |
14 | // 桶中结构转化为红黑树对应的table的最小大小 |
15 | static final int MIN_TREEIFY_CAPACITY = 64; |
16 | // 存储元素的数组,总是2的幂次倍 |
17 | transient Node<k,v>[] table; |
18 | // 存放具体元素的集 |
19 | transient Set<map.entry<k,v>> entrySet; |
20 | // 存放元素的个数,注意这个不等于数组的长度。 |
21 | transient int size; |
22 | // 每次扩容和更改map结构的计数器 |
23 | transient int modCount; |
24 | // 临界值 当实际大小(容量*填充因子)超过临界值时,会进行扩容 |
25 | int threshold; |
26 | // 加载因子 |
27 | final float loadFactor; |
28 | } |
loadFactor 加载因子
loadFactor加载因子是控制数组存放数据的疏密程度,loadFactor越趋近于1,那么 数组中存放的数据(entry)也就越多,也就越密,也就是会让链表的长度增加,loadFactor越小,也就是趋近于0,数组中存放的数据(entry)也就越少,也就越稀疏。
loadFactor太大导致查找元素效率低,太小导致数组的利用率低,存放的数据会很分散。loadFactor的默认值为0.75f是官方给出的一个比较好的临界值。
给定的默认容量为 16,负载因子为 0.75。Map 在使用过程中不断的往里面存放数据,当数量达到了 16 * 0.75 = 12 就需要将当前 16 的容量进行扩容,而扩容这个过程涉及到 rehash、复制数据等操作,所以非常消耗性能。
threshold 阈值
衡量数组是否需要扩增的一个标准:threshold = capacity * loadFactor,当Size>=threshold的时候,那么就要考虑对数组的扩增了。
Node 节点
1 | // 继承自 Map.Entry<K,V> |
2 | static class Node<K,V> implements Map.Entry<K,V> { |
3 | final int hash;// 哈希值,存放元素到hashmap中时用来与其他元素hash值比较 |
4 | final K key;//键 |
5 | V value;//值 |
6 | // 指向下一个节点 |
7 | Node<K,V> next; |
8 | Node(int hash, K key, V value, Node<K,V> next) { |
9 | this.hash = hash; |
10 | this.key = key; |
11 | this.value = value; |
12 | this.next = next; |
13 | } |
14 | public final K getKey() { return key; } |
15 | public final V getValue() { return value; } |
16 | public final String toString() { return key + "=" + value; } |
17 | // 重写hashCode()方法 |
18 | public final int hashCode() { |
19 | return Objects.hashCode(key) ^ Objects.hashCode(value); |
20 | } |
21 | |
22 | public final V setValue(V newValue) { |
23 | V oldValue = value; |
24 | value = newValue; |
25 | return oldValue; |
26 | } |
27 | // 重写 equals() 方法 |
28 | public final boolean equals(Object o) { |
29 | if (o == this) |
30 | return true; |
31 | if (o instanceof Map.Entry) { |
32 | Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
33 | if (Objects.equals(key, e.getKey()) && |
34 | Objects.equals(value, e.getValue())) |
35 | return true; |
36 | } |
37 | return false; |
38 | } |
39 | } |
TreeNode 树节点
1 | static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> { |
2 | TreeNode<K,V> parent; // 父 |
3 | TreeNode<K,V> left; // 左 |
4 | TreeNode<K,V> right; // 右 |
5 | TreeNode<K,V> prev; // needed to unlink next upon deletion |
6 | boolean red; // 判断颜色 |
7 | TreeNode(int hash, K key, V val, Node<K,V> next) { |
8 | super(hash, key, val, next); |
9 | } |
10 | // 返回根节点 |
11 | final TreeNode<K,V> root() { |
12 | for (TreeNode<K,V> r = this, p;;) { |
13 | if ((p = r.parent) == null) |
14 | return r; |
15 | r = p; |
16 | } |
源码分析
构造方法
HashMap 中有四个构造方法,它们分别如下:
1 | // 默认构造函数。 |
2 | public HashMap() { |
3 | this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted |
4 | } |
5 | |
6 | // 包含另一个“Map”的构造函数 |
7 | public HashMap(Map<? extends K, ? extends V> m) { |
8 | this.loadFactor = DEFAULT_LOAD_FACTOR; |
9 | putMapEntries(m, false);//下面会分析到这个方法 |
10 | } |
11 | |
12 | // 指定“容量大小”的构造函数 |
13 | public HashMap(int initialCapacity) { |
14 | this(initialCapacity, DEFAULT_LOAD_FACTOR); |
15 | } |
16 | |
17 | // 指定“容量大小”和“加载因子”的构造函数 |
18 | public HashMap(int initialCapacity, float loadFactor) { |
19 | if (initialCapacity < 0) |
20 | throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity); |
21 | if (initialCapacity > MAXIMUM_CAPACITY) |
22 | initialCapacity = MAXIMUM_CAPACITY; |
23 | if (loadFactor <= 0 || Float.isNaN(loadFactor)) |
24 | throw new IllegalArgumentException("Illegal load factor: " + loadFactor); |
25 | this.loadFactor = loadFactor; |
26 | this.threshold = tableSizeFor(initialCapacity); |
27 | } |
putMapEntries 方法
1 | final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) { |
2 | int s = m.size(); |
3 | if (s > 0) { |
4 | // 判断table是否已经初始化 |
5 | if (table == null) { // pre-size |
6 | // 未初始化,s为m的实际元素个数 |
7 | float ft = ((float)s / loadFactor) + 1.0F; |
8 | int t = ((ft < (float)MAXIMUM_CAPACITY) ? |
9 | (int)ft : MAXIMUM_CAPACITY); |
10 | // 计算得到的t大于阈值,则初始化阈值 |
11 | if (t > threshold) |
12 | threshold = tableSizeFor(t); |
13 | } |
14 | // 已初始化,并且m元素个数大于阈值,进行扩容处理 |
15 | else if (s > threshold) |
16 | resize(); |
17 | // 将m中的所有元素添加至HashMap中 |
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 | } |
put 方法
HashMap只提供了put用于添加元素,putVal方法只是给put方法调用的一个方法,并没有提供给用户使用。
1 | public V put(K key, V value) { |
2 | return putVal(hash(key), key, value, false, true); |
3 | } |
对putVal方法添加元素的分析如下:
- 如果定位到的数组位置没有元素 就直接插入。
- 如果定位到的数组位置有元素就和要插入的key比较,如果key相同就直接覆盖,如果key不相同,就判断p是否是一个树节点,如果是就调用
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value)
将元素添加进入。如果不是就遍历链表插入(插入的是链表尾部)。
具体流程图:
note:直接覆盖之后应该就会 return,不会有后续操作。参考 JDK8 HashMap.java 658 行。
- 过程分析
①.判断键值对数组table[i]是否为空或为null,否则执行resize()进行扩容;
②.根据键值key计算hash值得到插入的数组索引i,如果table[i]==null,直接新建节点添加,转向⑥,如果table[i]不为空,转向③;
③.判断table[i]的首个元素是否和key一样,如果相同直接覆盖value,否则转向④,这里的相同指的是hashCode以及equals;
④.判断table[i] 是否为treeNode,即table[i] 是否是红黑树,如果是红黑树,则直接在树中插入键值对,否则转向⑤;
⑤.遍历table[i],判断链表长度是否大于8,大于8的话把链表转换为红黑树,在红黑树中执行插入操作,否则进行链表的插入操作;遍历过程中若发现key已经存在直接覆盖value即可;
⑥.插入成功后,判断实际存在的键值对数量size是否超多了最大容量threshold,如果超过,进行扩容。
1 | final V putVal(int hash, K key, V value, boolean onlyIfAbsent, |
2 | boolean evict) { |
3 | Node<K,V>[] tab; Node<K,V> p; int n, i; |
4 | // table未初始化或者长度为0,进行扩容 |
5 | if ((tab = table) == null || (n = tab.length) == 0) |
6 | n = (tab = resize()).length; |
7 | // (n - 1) & hash 确定元素存放在哪个桶中,桶为空,新生成结点放入桶中(此时,这个结点是放在数组中) |
8 | if ((p = tab[i = (n - 1) & hash]) == null) |
9 | tab[i] = newNode(hash, key, value, null); |
10 | // 桶中已经存在元素 |
11 | else { |
12 | Node<K,V> e; K k; |
13 | // 比较桶中第一个元素(数组中的结点)的hash值相等,key相等 |
14 | if (p.hash == hash && |
15 | ((k = p.key) == key || (key != null && key.equals(k)))) |
16 | // 将第一个元素赋值给e,用e来记录 |
17 | e = p; |
18 | // hash值不相等,即key不相等;为红黑树结点 |
19 | else if (p instanceof TreeNode) |
20 | // 放入树中 |
21 | e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); |
22 | // 为链表结点 |
23 | else { |
24 | // 在链表最末插入结点 |
25 | for (int binCount = 0; ; ++binCount) { |
26 | // 到达链表的尾部 |
27 | if ((e = p.next) == null) { |
28 | // 在尾部插入新结点 |
29 | p.next = newNode(hash, key, value, null); |
30 | // 结点数量达到阈值,转化为红黑树 |
31 | if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st |
32 | treeifyBin(tab, hash); |
33 | // 跳出循环 |
34 | break; |
35 | } |
36 | // 判断链表中结点的key值与插入的元素的key值是否相等 |
37 | if (e.hash == hash && |
38 | ((k = e.key) == key || (key != null && key.equals(k)))) |
39 | // 相等,跳出循环 |
40 | break; |
41 | // 用于遍历桶中的链表,与前面的e = p.next组合,可以遍历链表 |
42 | p = e; |
43 | } |
44 | } |
45 | // 表示在桶中找到key值、hash值与插入元素相等的结点 |
46 | if (e != null) { |
47 | // 记录e的value |
48 | V oldValue = e.value; |
49 | // onlyIfAbsent为false或者旧值为null |
50 | if (!onlyIfAbsent || oldValue == null) |
51 | //用新值替换旧值 |
52 | e.value = value; |
53 | // 访问后回调 |
54 | afterNodeAccess(e); |
55 | // 返回旧值 |
56 | return oldValue; |
57 | } |
58 | } |
59 | // 结构性修改 |
60 | ++modCount; |
61 | // 实际大小大于阈值则扩容 |
62 | if (++size > threshold) |
63 | resize(); |
64 | // 插入后回调 |
65 | afterNodeInsertion(evict); |
66 | return null; |
67 | } |
get 方法
1 | public V get(Object key) { |
2 | Node<K,V> e; |
3 | return (e = getNode(hash(key), key)) == null ? null : e.value; |
4 | } |
5 | |
6 | final Node<K,V> getNode(int hash, Object key) { |
7 | Node<K,V>[] tab; Node<K,V> first, e; int n; K k; |
8 | if ((tab = table) != null && (n = tab.length) > 0 && |
9 | (first = tab[(n - 1) & hash]) != null) { |
10 | // 数组元素相等 |
11 | if (first.hash == hash && // always check first node |
12 | ((k = first.key) == key || (key != null && key.equals(k)))) |
13 | return first; |
14 | // 桶中不止一个节点 |
15 | if ((e = first.next) != null) { |
16 | // 在树中get |
17 | if (first instanceof TreeNode) |
18 | return ((TreeNode<K,V>)first).getTreeNode(hash, key); |
19 | // 在链表中get |
20 | do { |
21 | if (e.hash == hash && |
22 | ((k = e.key) == key || (key != null && key.equals(k)))) |
23 | return e; |
24 | } while ((e = e.next) != null); |
25 | } |
26 | } |
27 | return null; |
28 | } |
resize 方法
进行扩容,会伴随着一次重新hash分配,并且会遍历hash表中所有的元素,是非常耗时的。在编写程序中,要尽量避免resize。
1 | final 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 | // 超过最大值就不再扩充了,就只好随你碰撞去吧 |
8 | if (oldCap >= MAXIMUM_CAPACITY) { |
9 | threshold = Integer.MAX_VALUE; |
10 | return oldTab; |
11 | } |
12 | // 没超过最大值,就扩充为原来的2倍 |
13 | else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY) |
14 | newThr = oldThr << 1; // double threshold |
15 | } |
16 | else if (oldThr > 0) // initial capacity was placed in threshold |
17 | newCap = oldThr; |
18 | else { |
19 | // signifies using defaults |
20 | newCap = DEFAULT_INITIAL_CAPACITY; |
21 | newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); |
22 | } |
23 | // 计算新的resize上限 |
24 | if (newThr == 0) { |
25 | float ft = (float)newCap * loadFactor; |
26 | newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ? (int)ft : Integer.MAX_VALUE); |
27 | } |
28 | threshold = newThr; |
29 | "rawtypes","unchecked"}) ({ |
30 | Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; |
31 | table = newTab; |
32 | if (oldTab != null) { |
33 | // 把每个bucket都移动到新的buckets中 |
34 | for (int j = 0; j < oldCap; ++j) { |
35 | Node<K,V> e; |
36 | if ((e = oldTab[j]) != null) { |
37 | oldTab[j] = null; |
38 | if (e.next == null) |
39 | newTab[e.hash & (newCap - 1)] = e; |
40 | else if (e instanceof TreeNode) |
41 | ((TreeNode<K,V>)e).split(this, newTab, j, oldCap); |
42 | else { |
43 | Node<K,V> loHead = null, loTail = null; |
44 | Node<K,V> hiHead = null, hiTail = null; |
45 | Node<K,V> next; |
46 | do { |
47 | next = e.next; |
48 | // 原索引 |
49 | if ((e.hash & oldCap) == 0) { |
50 | if (loTail == null) |
51 | loHead = e; |
52 | else |
53 | loTail.next = e; |
54 | loTail = e; |
55 | } |
56 | // 原索引+oldCap |
57 | else { |
58 | if (hiTail == null) |
59 | hiHead = e; |
60 | else |
61 | hiTail.next = e; |
62 | hiTail = e; |
63 | } |
64 | } while ((e = next) != null); |
65 | // 原索引放到bucket里 |
66 | if (loTail != null) { |
67 | loTail.next = null; |
68 | newTab[j] = loHead; |
69 | } |
70 | // 原索引+oldCap放到bucket里 |
71 | if (hiTail != null) { |
72 | hiTail.next = null; |
73 | newTab[j + oldCap] = hiHead; |
74 | } |
75 | } |
76 | } |
77 | } |
78 | } |
79 | return newTab; |
80 | } |
问题:
HashMap中hash函数是如何实现的?
i. 高16bit不变,低16bit和高16bit做了一个异或 得到hash
ii. (n-1) & hash —>得到下标
抛开HashMap,hash冲突有哪些解决办法?
- 开放地址法,链地址法
针对HashMap中某个Entry链太长,查找的时间复杂度可能达到O(n),怎么优化
- 将链表转为红黑树,JDK1.8已经实现了,查找复杂度为log(n)
hashMap 的初始化容量为什么时2 的n次方?
1 | // 默认的初始容量是16 |
2 | static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; |
归宿到元素在 数组中的位置算法,及hash方法
1 | //1. 算出hash, |
2 | //2. hash 值与数组的长度的安位与 |
3 | int 元素的位置(数组下标)=(length-1)&hash; |
hash值的大小是一个32位的二进制数,怎么样才能转换成数组的下标呢?
思考,怎样将 一个整数的数值转换成 0~n-1的数值呢?
我们首先想到:取模(求余):i%n
但是存在问题:
- 负数首先要变成正数,才能求余(负数求余还是负数)
- 求余运算比位移运算要慢
2的n次方转换为2进制:
- 2^n:
1 | 2^1-->10 |
2 | |
3 | 2^2--->100 |
4 | |
5 | 2^3--->1000 |
6 | |
7 | 2^n---->100000... |
- 2^n-1
1 | 2^1-1-->1 |
2 | |
3 | 2^2-1--->11 |
4 | |
5 | 2^3-1--->111 |
6 | |
7 | 2^n-1---->111111... |
- hash 值为一个32位的二进制数据
(length-1)&hash
操作后:
1 | length-1: 111111 |
2 | hash: 1010100010 |
3 | ---------------------- |
4 | 1000010 ---> 数组下标 |
数组的初始容量是2^n的目标是:为了保证 得到的二进制的数值都是1组成的,如果不是2^n,则转换为2进制后,部分位置不都是都是1(如:11011),则不管是任何hash值和 它按位与,则会导致为0的那一位,永远都是0,最终,会导致 获取到的数组的下标不均匀,有些位置永远会空着。
10000…
只有长度为2的N次方时,我们
- 创建 hashMap 使用 有参构造时,指定容量不是 2 的n次方时,默认会调整为 2的n次方。
1 | public 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 | //对于给定的大小,返回两倍大小的的min |
14 | static final int tableSizeFor(int cap) { |
15 | int n = cap - 1; |
16 | n |= n >>> 1; |
17 | n |= n >>> 2; |
18 | n |= n >>> 4; |
19 | n |= n >>> 8; |
20 | n |= n >>> 16; |
21 | return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; |
22 | } |
所以,当我门初始化HashMap 时,指定容量不是2的n次方。HashMap 底层还是会转换为2的n次方。
jdk1.7 HashMap 在多线程情况下出现死锁问题:
陈浩:https://coolshell.cn/articles/9606.html
java 8 比java 7 的改进
java8 | java7 |
---|---|
数组+ 链表/红黑树 | 数组+链表 |
扩容时,插入方法改进为尾插法 | 头插法插入数据(多线程死循环) |
lamda 表达式 | 无 |
为什么每个桶链表的长度超过8 之后才转换为红黑树
泊松分布:每个桶中的数据超高8 的概率特别小,大约为 0.00000006。less than 1 in ten million
源码位置:188line
hash
知识点:异或:不进位的加法