ConcurrentHashMap.newKeySet()与Collections.newSetFromMap() [英] ConcurrentHashMap.newKeySet() vs Collections.newSetFromMap()
问题描述
Java 8引入了获取并发Set
实现的新方法
Java 8 introduced new way to obtain a concurrent Set
implementation
// Pre-Java-8 way to create a concurrent set
Set<String> oldStyle = Collections.newSetFromMap(new ConcurrentHashMap<>());
// New method in Java 8
Set<String> newStyle = ConcurrentHashMap.newKeySet();
有什么理由喜欢新方法吗?
Is there any reason to prefer new method?
有什么优点/缺点吗?
推荐答案
ConcurrentHashMap.newKeySet()
应该更有效,因为它删除了一个间接级别. Collections.newSetFromMap(map)
主要基于将操作重定向到map.keySet()
,但是ConcurrentHashMap.newKeySet()
与map.keySet()
本身非常接近(仅具有附加支持).
ConcurrentHashMap.newKeySet()
should be somewhat more efficient as removes a single level of indirection. Collections.newSetFromMap(map)
is mostly based on redirecting the operations to the map.keySet()
, but ConcurrentHashMap.newKeySet()
is very close to map.keySet()
itself (just with additions support).
关于功能,我认为没什么不同.
As for functionality, I see no difference.
这篇关于ConcurrentHashMap.newKeySet()与Collections.newSetFromMap()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!