mirror of
https://github.com/Snailclimb/JavaGuide
synced 2025-06-16 18:10:13 +08:00
Update hashmap-source-code.md
This commit is contained in:
parent
b0cb31e2f6
commit
b8247a3e2d
@ -274,19 +274,18 @@ final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
|
|||||||
// (n - 1) & hash 确定元素存放在哪个桶中,桶为空,新生成结点放入桶中(此时,这个结点是放在数组中)
|
// (n - 1) & hash 确定元素存放在哪个桶中,桶为空,新生成结点放入桶中(此时,这个结点是放在数组中)
|
||||||
if ((p = tab[i = (n - 1) & hash]) == null)
|
if ((p = tab[i = (n - 1) & hash]) == null)
|
||||||
tab[i] = newNode(hash, key, value, null);
|
tab[i] = newNode(hash, key, value, null);
|
||||||
// 桶中已经存在元素
|
// 桶中已经存在元素(处理hash冲突)
|
||||||
else {
|
else {
|
||||||
Node<K,V> e; K k;
|
Node<K,V> e; K k;
|
||||||
// 比较桶中第一个元素(数组中的结点)的hash值相等,key相等
|
// 判断table[i]中的元素是否与插入的key一样,若相同那就直接使用插入的值p替换掉旧的值e。
|
||||||
if (p.hash == hash &&
|
if (p.hash == hash &&
|
||||||
((k = p.key) == key || (key != null && key.equals(k))))
|
((k = p.key) == key || (key != null && key.equals(k))))
|
||||||
// 将第一个元素赋值给e,用e来记录
|
|
||||||
e = p;
|
e = p;
|
||||||
// hash值不相等,即key不相等;为红黑树结点
|
// 判断插入的是否是红黑树节点
|
||||||
else if (p instanceof TreeNode)
|
else if (p instanceof TreeNode)
|
||||||
// 放入树中
|
// 放入树中
|
||||||
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
|
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
|
||||||
// 为链表结点
|
// 不是红黑树节点则说明为链表结点
|
||||||
else {
|
else {
|
||||||
// 在链表最末插入结点
|
// 在链表最末插入结点
|
||||||
for (int binCount = 0; ; ++binCount) {
|
for (int binCount = 0; ; ++binCount) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user