Skip to content

Commit

Permalink
Update hashmap-source-code.md
Browse files Browse the repository at this point in the history
改动行数265
将put流程图更新,地址是用的阿里云oss
  • Loading branch information
Davin-Lee authored Jul 13, 2023
1 parent f69d399 commit 1a7249d
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions docs/java/collection/hashmap-source-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,8 @@ HashMap 只提供了 put 用于添加元素,putVal 方法只是给 put 方法
1. 如果定位到的数组位置没有元素 就直接插入。
2. 如果定位到的数组位置有元素就和要插入的 key 比较,如果 key 相同就直接覆盖,如果 key 不相同,就判断 p 是否是一个树节点,如果是就调用`e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value)`将元素添加进入。如果不是就遍历链表插入(插入的是链表尾部)。

![ ](https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-7/put方法.png)
![ ](https://qingcheng-davin.oss-cn-shenzhen.aliyuncs.com/put.png)

说明:上图有两个小问题:

- 直接覆盖之后应该就会 return,不会有后续操作。参考 JDK8 HashMap.java 658 行([issue#608](https://github.com/Snailclimb/JavaGuide/issues/608))。
- 当链表长度大于阈值(默认为 8)并且 HashMap 数组长度超过 64 的时候才会执行链表转红黑树的操作,否则就只是对数组扩容。参考 HashMap 的 `treeifyBin()` 方法([issue#1087](https://github.com/Snailclimb/JavaGuide/issues/1087))。

```java
public V put(K key, V value) {
Expand Down

0 comments on commit 1a7249d

Please sign in to comment.