How would I convert a List, (let's say an ArrayList) to a ConcurrentSet in Java?
I'm aware of this answer: Easiest way to convert a List to a Set in Java But I can't seem to get it to work for a ConcurrentSet.
How would I convert a List, (let's say an ArrayList) to a ConcurrentSet in Java?
I'm aware of this answer: Easiest way to convert a List to a Set in Java But I can't seem to get it to work for a ConcurrentSet.
You can do a conversion like this.
public static <E> Set<E> toConcurrentSet(Collection<E> coll) {
Set<E> set = Collections.newSetFromMap(new ConcurrentHashMap<>());
set.addAll(coll);
return set;
}
If with ConcurrentSet you mean a set that can be accessed concurrently, you can try:
Set<Foo> foo = Collections.synchronizedSet(new HashSet<Foo>(myList));
The returned set will be thread safe.