Skip to content

Commit b8247a3

Browse files
committed
Update hashmap-source-code.md
1 parent b0cb31e commit b8247a3

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

docs/java/collection/hashmap-source-code.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -274,19 +274,18 @@ final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
274274
// (n - 1) & hash 确定元素存放在哪个桶中,桶为空,新生成结点放入桶中(此时,这个结点是放在数组中)
275275
if ((p = tab[i = (n - 1) & hash]) == null)
276276
tab[i] = newNode(hash, key, value, null);
277-
// 桶中已经存在元素
277+
// 桶中已经存在元素(处理hash冲突)
278278
else {
279279
Node<K,V> e; K k;
280-
// 比较桶中第一个元素(数组中的结点)的hash值相等,key相等
280+
// 判断table[i]中的元素是否与插入的key一样,若相同那就直接使用插入的值p替换掉旧的值e。
281281
if (p.hash == hash &&
282282
((k = p.key) == key || (key != null && key.equals(k))))
283-
// 将第一个元素赋值给e,用e来记录
284283
e = p;
285-
// hash值不相等,即key不相等;为红黑树结点
284+
// 判断插入的是否是红黑树节点
286285
else if (p instanceof TreeNode)
287286
// 放入树中
288287
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
289-
// 为链表结点
288+
// 不是红黑树节点则说明为链表结点
290289
else {
291290
// 在链表最末插入结点
292291
for (int binCount = 0; ; ++binCount) {

0 commit comments

Comments
 (0)