带有 Guava 集合的线程安全 HashSet
就像标题所说,我想使用 Guava Collections 获得一个线程安全的 HashSet。
有可用的吗?
Like the title says, i would like to get a thread-safe HashSet using Guava Collections.
Are any available?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这将是正确的答案,使用 Guava 的 Sets 类。不管怎样,@crhis 的回答是好的。
This would be the right answer, Using the Sets class from Guava. Anyway the answer from @crhis was good intended.
Java 8 引入了创建并发哈希集的新方法 - ConcurrentHashMap.newKeySet()
它比包装在
Collections.newSetFromMap
中要短Java 8 introduces new way to create concurrent hash set - ConcurrentHashMap.newKeySet()
It's shorter than wrapping in
Collections.newSetFromMap
Google Collections有一个名为Sets.newConcurrentHashSet() 一段时间。
它的实现与 Chris 的建议类似:
他们在 com.google.common.collect.Sets 类中有一个 newSetFromMap() 方法(由 Doug Lea 在成员的帮助下编写) JCP JSR-166)。该方法已添加到 java 1.6 中的 java.util.Collections 中。
它在 Google Collections 1.0rc1 中被撤回,因为有计划更好地支持 Guava 中的并发集(更多信息此处)。
这篇文章扩展了使用构造并发集的“newSetFromMap”方法。
Google Collections had a factory method named Sets.newConcurrentHashSet() for a while.
Its implementation was similar to Chris's suggestion:
They had a newSetFromMap() method inside the com.google.common.collect.Sets class (written by Doug Lea with assistance from members of JCP JSR-166). That method was added to java.util.Collections in java 1.6.
It was withdrawn in Google Collections 1.0rc1, since there are plans to better support concurrent sets in Guava (more information here).
This post expands on the use of the "newSetFromMap" method to construct concurrent sets.