The thread-safe version of a Set is CopyOnWriteArraySet. The following table enumerates the Java built-in single-threaded and multithreaded sets:
Single thread |
Multithreaded |
HashSet TreeSet (sorted set) LinkedHashSet (maintain insertions order) BitSet EnumSet |
ConcurrentSkipListSet (sorted set) CopyOnWriteArraySet (often reads, seldom updates) |
This is a Set that uses an internal CopyOnWriteArrayList for all of its operations. Creating such a Set can be done as follows:
Set<Integer> set = new CopyOnWriteArraySet<>();
Use this collection when reads are frequent and changes are seldom.
The thread-safe version of NavigableSet is ConcurrentSkipListSet (concurrent SortedSet implementation, with most basic operations in O(log n)).