Skip to content

Commit ee3b95b

Browse files
authored
Merge pull request Snailclimb#2092 from andanyoung/patch-1
Update hashmap-source-code.md
2 parents 9c24c18 + 1458e0a commit ee3b95b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,24 @@ public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneabl
113113

114114
**threshold = capacity \* loadFactor****当 Size>threshold**的时候,那么就要考虑对数组的扩增了,也就是说,这个的意思就是 **衡量数组是否需要扩增的一个标准**
115115

116+
### <font color='red'>示例</font>
117+
118+
`Map<Object, Object> map = new HashMap<>();`
119+
120+
- 刚初始化情况下,`table`为空,`threshold = 0`,`size=0`(size 为实际存储数据大小)
121+
122+
- 当调用`put`方法时,会调用 `resize()` 方法扩容,库容到 table 大小为 16 `threshold = (int)(*DEFAULT_LOAD_FACTOR* * *DEFAULT_INITIAL_CAPACITY*)` = 16 x 0.75 = 12
123+
124+
- 是否要扩容判断
125+
126+
调用 `put`方法 -> `putVal` 添加完后会进行扩容校验
127+
128+
```
129+
++modCount;
130+
if (++size > threshold)
131+
resize();
132+
```
133+
116134
**Node 节点类源码:**
117135

118136
```java

0 commit comments

Comments
 (0)