14
Feb
2011
ConcurrentHashSet in Java from ConcurrentHashMap
Problem
While you do have a ConcurrentHashMap class in Java, there is no ConcurrentHashSet.
Solution
You can easily get a ConcurrentHashSet with the following code -
Collections.newSetFromMap(new ConcurrentHashMap<Object,Boolean>())
Notes
- A Set lends itself to implementation via a Map if you think about it. So can actually just use a Map. But that may not fit in well with the context of your use.
- The HashSet class internally uses a HashMap.
- The ConcurrentHashSet obtained via the method inherits pretty much all the concurrency features of the underlying collection.
- See api-docs for newSetFromMap
No related posts.
Tags: Concurrency, Java
This entry was posted
on Monday, February 14th, 2011 at 3:19 am and is filed under Uncategorized.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
