HashSet is based on HashMap.
If we look at HashSet<E>
implementation, everything is been managed under HashMap<E,Object>
.
<E>
is used as a key of HashMap
.
And we know that HashMap
is not thread safe. That is why we have ConcurrentHashMap
in Java.
Based on this, I am confused that why we don't have a ConcurrentHashSet which should be based on the ConcurrentHashMap
?
Is there anything else that I am missing? I need to use Set
in a multi-threaded environment.
Also, If I want to create my own ConcurrentHashSet
can I achieve it by just replacing the HashMap
to ConcurrentHashMap
and leaving the rest as is?
This question is related to
java
collections
concurrency
hashmap
hashset
Set<String> mySet = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class ConcurrentHashSet<E> extends AbstractSet<E> implements Set<E>{
private final ConcurrentMap<E, Object> theMap;
private static final Object dummy = new Object();
public ConcurrentHashSet(){
theMap = new ConcurrentHashMap<E, Object>();
}
@Override
public int size() {
return theMap.size();
}
@Override
public Iterator<E> iterator(){
return theMap.keySet().iterator();
}
@Override
public boolean isEmpty(){
return theMap.isEmpty();
}
@Override
public boolean add(final E o){
return theMap.put(o, ConcurrentHashSet.dummy) == null;
}
@Override
public boolean contains(final Object o){
return theMap.containsKey(o);
}
@Override
public void clear(){
theMap.clear();
}
@Override
public boolean remove(final Object o){
return theMap.remove(o) == ConcurrentHashSet.dummy;
}
public boolean addIfAbsent(final E o){
Object obj = theMap.putIfAbsent(o, ConcurrentHashSet.dummy);
return obj == null;
}
}
It looks like Java provides a concurrent Set implementation with its ConcurrentSkipListSet. A SkipList Set is just a special kind of set implementation. It still implements the Serializable, Cloneable, Iterable, Collection, NavigableSet, Set, SortedSet interfaces. This might work for you if you only need the Set interface.
Why not use: CopyOnWriteArraySet from java.util.concurrent?
As pointed by this the best way to obtain a concurrency-able HashSet is by means of Collections.synchronizedSet()
Set s = Collections.synchronizedSet(new HashSet(...));
This worked for me and I haven't seen anybody really pointing to it.
EDIT This is less efficient than the currently aproved solution, as Eugene points out, since it just wraps your set into a synchronized decorator, while a ConcurrentHashMap
actually implements low-level concurrency and it can back your Set just as fine. So thanks to Mr. Stepanenkov for making that clear.
http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#synchronizedSet-java.util.Set-
You can use guava's Sets.newSetFromMap(map)
to get one. Java 6 also has that method in java.util.Collections
Like Ray Toal mentioned it is as easy as:
Set<String> myConcurrentSet = ConcurrentHashMap.newKeySet();
With Guava 15 you can also simply use:
Set s = Sets.newConcurrentHashSet();
Source: Stackoverflow.com