We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent da78013 commit 512b8dbCopy full SHA for 512b8db
1 file changed
MD/HashMap.md
@@ -20,4 +20,14 @@
20
21
## get 方法
22
23
-get 和 put 类似,也是将传入的 Key 计算出 index ,如果该位置上是一个链表就需要遍历整个链表
+get 和 put 类似,也是将传入的 Key 计算出 index ,如果该位置上是一个链表就需要遍历整个链表,通过 `key.equals(k)` 来找到对应的元素。
24
+
25
+## notice
26
27
+在并发环境下使用 HashMap 容易出现死循环。当发生扩容时,调用 `resize()` 方法里的 `rehash()` 时,容易出现环形链表。这样当获取一个不存在的 `key` 时,计算出的 `index` 正好是环形链表的下标时就会出现死循环。
28
29
+
30
31
+> 所以 HashMap 只能在单线程中使用,并且尽量的预设容量,尽可能的减少扩容。
32
33
+多线程场景下推荐使用 [ConcurrentHashMap](https://github.com/crossoverJie/Java-Interview/blob/master/MD/ConcurrentHashMap.md)。
0 commit comments