HashMap记录
2019-04-10 本文已影响0人
AlanSun2
关于红黑树
// 这是一个阈值,当桶(bucket)上的链表数大于等于这个值时会转成红黑树,put方法的代码里有用到。put的时候如果两个值
// 经过hash后都落在同一个bucket上这叫发生一次hash碰撞,往里不断put数据,当一个bucket发生8次碰撞就转成红黑树。
static final int TREEIFY_THRESHOLD = 8;
// 也是阈值同上一个相反,当桶(bucket)上的链表数小于这个值时树转链表
static final int UNTREEIFY_THRESHOLD = 6;
put方法:
/**
* Implements Map.put and related methods
*
* @param hash hash for key
* @param key the key
* @param value the value to put
* @param onlyIfAbsent 如果为true,则当发生碰撞时hash值相同,key也相同,则newValue不替换oldValue
* @param evict if false, the table is in creation mode.
* @return previous value, or null if none
*/
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) // 判断是否有碰撞,如果i指定的bin为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;
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 判断当前bin的数量是否>=8,if true,则将链表转换为红黑树
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 发生碰撞时,hash值相同,key值相同,e就不为null
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null) //判断是否需要将newValue赋值给oldValue
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold) //判断是否需要扩容,初始的threshold为12,这里的size不是bin的数量,而是key-value对的数量
resize();
afterNodeInsertion(evict);
return null;
}
(n - 1) & hash
等价于 hash % n
,这里也就是对hash
求余。这也是为什么初始化的HashMap为什么是 1<<4的原因,因为n
必须是2^n。
get方法:
/**
* Implements Map.get and related methods
*
* @param hash hash for key
* @param key the key
* @return the node, or null if none
*/
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))))
return first;
if ((e = first.next) != null) {
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;
}
- 首先将 key hash 之后取得所定位的桶。
- 如果桶为空则直接返回 null 。
- 否则判断桶的第一个位置(有可能是链表、红黑树)的 key 是否为查询的 key,是就直接返回 value。
- 如果第一个不匹配,则判断它的下一个是红黑树还是链表。
- 红黑树就按照树的查找方式返回值。
- 不然就按照链表的方式遍历匹配返回值。
从这两个核心方法(get/put)可以看出 1.8 中对大链表做了优化,修改为红黑树之后查询效率直接提高到了 O(logn)。
- jdk8之后的HashMap已经没有链表死循环的问题的
参考链接
https://www.jianshu.com/p/281137bdc223
https://crossoverjie.top/2018/07/23/java-senior/ConcurrentHashMap/
compute,computeIfPresent,computeIfAbsent是jdk8加入的。
compute
传入一个key和一个二元操作方法。假设 key 对应的旧元素为 oldVal,通过二元操作方法后的值为 v
if oldVal != null && v != null,then 把 v 赋值给 key 的对应元素
if oldVal != null && v == null,then 把 key 移除
if oldVal == null && v != null,then 创建新元素并插入