Skip to content

Commit 512b8db

Browse files
committed
HashMap
1 parent da78013 commit 512b8db

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

MD/HashMap.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,14 @@
2020

2121
## get 方法
2222

23-
get 和 put 类似,也是将传入的 Key 计算出 index ,如果该位置上是一个链表就需要遍历整个链表
23+
get 和 put 类似,也是将传入的 Key 计算出 index ,如果该位置上是一个链表就需要遍历整个链表,通过 `key.equals(k)` 来找到对应的元素。
24+
25+
## notice
26+
27+
在并发环境下使用 HashMap 容易出现死循环。当发生扩容时,调用 `resize()` 方法里的 `rehash()` 时,容易出现环形链表。这样当获取一个不存在的 `key` 时,计算出的 `index` 正好是环形链表的下标时就会出现死循环。
28+
29+
![](https://ws2.sinaimg.cn/large/006tNc79gy1fn85u0a0d9j30n20ii0tp.jpg)
30+
31+
> 所以 HashMap 只能在单线程中使用,并且尽量的预设容量,尽可能的减少扩容。
32+
33+
多线程场景下推荐使用 [ConcurrentHashMap](https://github.com/crossoverJie/Java-Interview/blob/master/MD/ConcurrentHashMap.md)

0 commit comments

Comments
 (0)