CopyOnWriteArraySet

public class CopyOnWriteArraySet extends AbstractSet<E>
implements Serializable

A Set that uses an internal CopyOnWriteArrayList for all of its operations. Thus, it shares the same basic properties:

  • It is best suited for applications in which set sizes generally stay small, read-only operations vastly outnumber mutative operations, and you need to prevent interference among threads during traversal.
  • It is thread-safe.
  • Mutative operations (add, set, remove, etc.) are expensive since they usually entail copying the entire underlying array.
  • Iterators do not support the mutative remove operation.
  • Traversal via iterators is fast and cannot encounter interference from other threads. Iterators rely on unchanging snapshots of the array at the time the iterators were constructed.

Sample Usage. The following code sketch uses a copy-on-write set to maintain a set of Handler objects that perform some action upon state updates.

 class Handler { void handle(); ... }

 class X {
   private final CopyOnWriteArraySet<Handler> handlers
     = new CopyOnWriteArraySet<>();
   public void addHandler(Handler h) { handlers.add(h); }

   private long internalState;
   private synchronized void changeState() { internalState = ...; }

   public void update() {
     changeState();
     for (Handler handler : handlers)
       handler.handle();
   }
 }

Public Constructor Summary

CopyOnWriteArraySet()
Creates an empty set.
CopyOnWriteArraySet(Collection<? extends E> c)
Creates a set containing all of the elements of the specified collection.

Public Method Summary

boolean
add(E e)
Adds the specified element to this set if it is not already present.
boolean
addAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this set if they're not already present.
void
clear()
Removes all of the elements from this set.
boolean
contains(Object o)
Returns true if this set contains the specified element.
boolean