From fc06cf9ac49c8e0309f47bd328e55af57f05ee3e Mon Sep 17 00:00:00 2001 From: Jack_Lee Date: Sun, 11 Jun 2023 18:16:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9hashmap=E6=BA=90=E7=A0=81?= =?UTF-8?q?=E5=88=86=E6=9E=90=E6=96=87=E6=A1=A3=E4=B8=AD=E7=9A=84=20putVal?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E6=B5=81=E7=A8=8B=E5=9B=BE=EF=BC=8C=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=8E=9F=E5=9B=BE=E4=B8=8B=E6=96=B9=E7=9A=84=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=E6=96=87=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/java/collection/hashmap-source-code.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/docs/java/collection/hashmap-source-code.md b/docs/java/collection/hashmap-source-code.md index 5e82eaa7..ed394e4a 100644 --- a/docs/java/collection/hashmap-source-code.md +++ b/docs/java/collection/hashmap-source-code.md @@ -262,12 +262,9 @@ HashMap 只提供了 put 用于添加元素,putVal 方法只是给 put 方法 1. 如果定位到的数组位置没有元素 就直接插入。 2. 如果定位到的数组位置有元素就和要插入的 key 比较,如果 key 相同就直接覆盖,如果 key 不相同,就判断 p 是否是一个树节点,如果是就调用`e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value)`将元素添加进入。如果不是就遍历链表插入(插入的是链表尾部)。 -![ ](https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-7/put方法.png) +![ ](https://s1.ax1x.com/2023/06/11/pCVjnZ6.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) { -- Gitee