Skip to content

Commit 1ecd04c

Browse files
committed
add
1 parent 7bd1095 commit 1ecd04c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: ch16/06_Comparing_Map_Implementations.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
3+
4+
## 比较 Map 的实现
5+
6+
`16-1` 显示了 `Map` 的不同平台实现的相对性能(“next”栏显示迭代器在密钥集上的下一次操作的开销)。 与队列的实现一样,您对 `map` 类的选择可能更多地受到应用程序的功能需求和所需的并发属性的影响。
7+
8+
一些特殊情况决定了实现:`EnumMap` 应该总是(并且仅仅)用于从枚举映射。 诸如第 `16.2.4` 节描述的图遍历等问题需要 `IdentityHashMap`。 对于有排序的映射,请使用不需要线程安全性的 `TreeMap`,否则使用 `ConcurrentSkipListMap`
9+
10+
`16-1`。 不同 `Map` 实现的比较性能
11+
12+
get containsKey next Notes
13+
HashMap O(1) O(1) O(h/n) h is the table capacity
14+
LinkedHashMap O(1) O(1) O(1)
15+
IdentityHashMap O(1) O(1) O(h/n) h is the table capacity
16+
EnumMap O(1) O(1) O(1)
17+
TreeMap O(log n) O(log n) O(log n)
18+
ConcurrentHashMap O(1) O(1) O(h/n) h is the table capacity
19+
ConcurrentSkipListMap O(log n) O(log n) O(1)
20+
21+
这就为通用地图留下了实施的选择。 对于并发应用程序,`ConcurrentHashMap` 是唯一的选择。 否则,如果您需要使用映射的插入或访问顺序(例如,将其用作缓存),则可以优先使用 `HashMap` 上的 `LinkedHashMap`(并接受其稍差的性能)。
22+
23+
24+

0 commit comments

Comments
 (0)