# CopyOnWriteArraySet

public class **CopyOnWriteArraySet** extends [AbstractSet](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractSet.html)\<E\>  
implements [Serializable](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Serializable.html) A `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html` that uses an internal `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArrayList.html`
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();
       }
     }

<br />

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArrayList.html`

### Public Constructor Summary

|---|---|
|   | [CopyOnWriteArraySet](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#CopyOnWriteArraySet())() Creates an empty set. |
|   | [CopyOnWriteArraySet](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#CopyOnWriteArraySet(java.util.Collection<?%20extends%20E>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<? extends E\> c) Creates a set containing all of the elements of the specified collection. |

### Public Method Summary

|---|---|
| boolean | [add](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#add(E))(E e) Adds the specified element to this set if it is not already present. |
| boolean | [addAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#addAll(java.util.Collection<?%20extends%20E>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<? extends E\> c) Adds all of the elements in the specified collection to this set if they're not already present. |
| void | [clear](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#clear())() Removes all of the elements from this set. |
| boolean | [contains](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#contains(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o) Returns `true` if this set contains the specified element. |
| boolean | [containsAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#containsAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Returns `true` if this set contains all of the elements of the specified collection. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o) Compares the specified object with this set for equality. |
| void | [forEach](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#forEach(java.util.function.Consumer<?%20super%20E>))([Consumer](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/function/Consumer.html)\<? super E\> action) |
| boolean | [isEmpty](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#isEmpty())() Returns `true` if this set contains no elements. |
| [Iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Iterator.html)\<E\> | [iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#iterator())() Returns an iterator over the elements contained in this set in the order in which these elements were added. |
| boolean | [remove](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#remove(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o) Removes the specified element from this set if it is present. |
| boolean | [removeAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#removeAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Removes from this set all of its elements that are contained in the specified collection. |
| boolean | [removeIf](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#removeIf(java.util.function.Predicate<?%20super%20E>))([Predicate](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/function/Predicate.html)\<? super E\> filter) |
| boolean | [retainAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#retainAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Retains only the elements in this set that are contained in the specified collection. |
| int | [size](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#size())() Returns the number of elements in this set. |
| [Spliterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html)\<E\> | [spliterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#spliterator())() Returns a `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html` over the elements in this set in the order in which these elements were added. |
| \<T\> T\[\] | [toArray](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#toArray(T[]))(T\[\] a) Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. |
| [Object\[\]](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [toArray](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#toArray())() Returns an array containing all of the elements in this set. |

### Inherited Method Summary

From class [java.util.AbstractSet](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractSet.html)

|---|---|
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractSet.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o) Compares the specified object with this set for equality. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractSet.html#hashCode())() Returns the hash code value for this set. |
| boolean | [removeAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractSet.html#removeAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Removes from this set all of its elements that are contained in the specified collection (optional operation). |

From class [java.util.AbstractCollection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html)

|---|---|
| boolean | [add](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#add(E))(E e) Ensures that this collection contains the specified element (optional operation). This implementation always throws an UnsupportedOperationException. |
| boolean | [addAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#addAll(java.util.Collection<?%20extends%20E>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<? extends E\> c) Adds all of the elements in the specified collection to this collection (optional operation). This implementation iterates over the specified collection, and adds each object returned by the iterator to this collection, in turn. |
| void | [clear](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#clear())() Removes all of the elements from this collection (optional operation). This implementation iterates over this collection, removing each element using the Iterator.remove operation. |
| boolean | [contains](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#contains(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o) Returns true if this collection contains the specified element. This implementation iterates over the elements in the collection, checking each element in turn for equality with the specified element. |
| boolean | [containsAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#containsAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Returns true if this collection contains all of the elements in the specified collection. This implementation iterates over the specified collection, checking each element returned by the iterator in turn to see if it's contained in this collection. |
| boolean | [isEmpty](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#isEmpty())() Returns true if this collection contains no elements. This implementation returns size() == 0. |
| abstract [Iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Iterator.html)\<E\> | [iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#iterator())() Returns an iterator over the elements contained in this collection. |
| boolean | [remove](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#remove(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o) Removes a single instance of the specified element from this collection, if it is present (optional operation). This implementation iterates over the collection looking for the specified element. |
| boolean | [removeAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#removeAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Removes all of this collection's elements that are also contained in the specified collection (optional operation). This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection. |
| boolean | [retainAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#retainAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Retains only the elements in this collection that are contained in the specified collection (optional operation). This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection. |
| abstract int | [size](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#size())() Returns the number of elements in this collection. |
| \<T\> T\[\] | [toArray](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#toArray(T[]))(T\[\] contents) Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. This implementation returns an array containing all the elements returned by this collection's iterator in the same order, stored in consecutive elements of the array, starting with index `0`. |
| [Object\[\]](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [toArray](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#toArray())() Returns an array containing all of the elements in this collection. This implementation returns an array containing all the elements returned by this collection's iterator, in the same order, stored in consecutive elements of the array, starting with index `0`. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/AbstractCollection.html#toString())() Returns a string representation of this collection. |

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

From interface [java.util.Set](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html)

|---|---|
| abstract boolean | [add](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#add(E))(E e) Adds the specified element to this set if it is not already present (optional operation). |
| abstract boolean | [addAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#addAll(java.util.Collection<?%20extends%20E>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<? extends E\> c) Adds all of the elements in the specified collection to this set if they're not already present (optional operation). |
| abstract void | [clear](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#clear())() Removes all of the elements from this set (optional operation). |
| abstract boolean | [contains](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#contains(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o) Returns `true` if this set contains the specified element. |
| abstract boolean | [containsAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#containsAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Returns `true` if this set contains all of the elements of the specified collection. |
| abstract boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o) Compares the specified object with this set for equality. |
| abstract int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#hashCode())() Returns the hash code value for this set. |
| abstract boolean | [isEmpty](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#isEmpty())() Returns `true` if this set contains no elements. |
| abstract [Iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Iterator.html)\<E\> | [iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#iterator())() Returns an iterator over the elements in this set. |
| abstract boolean | [remove](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#remove(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o) Removes the specified element from this set if it is present (optional operation). |
| abstract boolean | [removeAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#removeAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Removes from this set all of its elements that are contained in the specified collection (optional operation). |
| abstract boolean | [retainAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#retainAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Retains only the elements in this set that are contained in the specified collection (optional operation). |
| abstract int | [size](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#size())() Returns the number of elements in this set (its cardinality). |
| [Spliterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html)\<E\> | [spliterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#spliterator())() Creates a `Spliterator` over the elements in this set. |
| abstract \<T\> T\[\] | [toArray](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#toArray(T[]))(T\[\] a) Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. |
| abstract [Object\[\]](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [toArray](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html#toArray())() Returns an array containing all of the elements in this set. |

From interface [java.util.Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)

|---|---|
| abstract boolean | [add](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#add(E))(E e) Ensures that this collection contains the specified element (optional operation). |
| abstract boolean | [addAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#addAll(java.util.Collection<?%20extends%20E>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<? extends E\> c) Adds all of the elements in the specified collection to this collection (optional operation). |
| abstract void | [clear](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#clear())() Removes all of the elements from this collection (optional operation). |
| abstract boolean | [contains](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#contains(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o) Returns true if this collection contains the specified element. |
| abstract boolean | [containsAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#containsAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Returns true if this collection contains all of the elements in the specified collection. |
| abstract boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o) Compares the specified object with this collection for equality. |
| abstract int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#hashCode())() Returns the hash code value for this collection. |
| abstract boolean | [isEmpty](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#isEmpty())() Returns true if this collection contains no elements. |
| abstract [Iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Iterator.html)\<E\> | [iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#iterator())() Returns an iterator over the elements in this collection. |
| [Stream](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/stream/Stream.html)\<E\> | [parallelStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#parallelStream())() Returns a possibly parallel `Stream` with this collection as its source. |
| abstract boolean | [remove](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#remove(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o) Removes a single instance of the specified element from this collection, if it is present (optional operation). |
| abstract boolean | [removeAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#removeAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
| boolean | [removeIf](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#removeIf(java.util.function.Predicate<?%20super%20E>))([Predicate](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/function/Predicate.html)\<? super E\> filter) Removes all of the elements of this collection that satisfy the given predicate. |
| abstract boolean | [retainAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#retainAll(java.util.Collection<?>))([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c) Retains only the elements in this collection that are contained in the specified collection (optional operation). |
| abstract int | [size](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#size())() Returns the number of elements in this collection. |
| [Spliterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html)\<E\> | [spliterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#spliterator())() Creates a `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html` over the elements in this collection. |
| [Stream](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/stream/Stream.html)\<E\> | [stream](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#stream())() Returns a sequential `Stream` with this collection as its source. |
| abstract \<T\> T\[\] | [toArray](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#toArray(T[]))(T\[\] a) Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
| abstract [Object\[\]](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [toArray](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#toArray())() Returns an array containing all of the elements in this collection. |

From interface [java.lang.Iterable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Iterable.html)

|---|---|
| void | [forEach](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Iterable.html#forEach(java.util.function.Consumer<?%20super%20T>))([Consumer](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/function/Consumer.html)\<? super T\> action) Performs the given action for each element of the `Iterable` until all elements have been processed or the action throws an exception. |
| abstract [Iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Iterator.html)\<E\> | [iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Iterable.html#iterator())() Returns an iterator over elements of type `T`. |
| [Spliterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html)\<E\> | [spliterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Iterable.html#spliterator())() Creates a `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html` over the elements described by this `Iterable`. |

## Public Constructors

#### public
**CopyOnWriteArraySet**
()

Creates an empty set.

#### public
**CopyOnWriteArraySet**
([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<? extends E\> c)

Creates a set containing all of the elements of the specified
collection.

##### Parameters

| c | the collection of elements to initially contain |
|---|---|

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the specified collection is null |
|---|---|

## Public Methods

#### public boolean
**add**
(E e)

Adds the specified element to this set if it is not already present.
More formally, adds the specified element `e` to this set if
the set contains no element `e2` such that
`Objects.equals(e, e2)`.
If this set already contains the element, the call leaves the set
unchanged and returns `false`.

##### Parameters

| e | element to be added to this set |
|---|---|

##### Returns

- `true` if this set did not already contain the specified element

#### public boolean
**addAll**
([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<? extends E\> c)

Adds all of the elements in the specified collection to this set if
they're not already present. If the specified collection is also a
set, the `addAll` operation effectively modifies this set so
that its value is the *union* of the two sets. The behavior of
this operation is undefined if the specified collection is modified
while the operation is in progress.

##### Parameters

| c | collection containing elements to be added to this set |
|---|---|

##### Returns

- `true` if this set changed as a result of the call

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the specified collection is null |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#add(E)`

#### public void
**clear**
()

Removes all of the elements from this set.
The set will be empty after this call returns.

#### public boolean
**contains**
([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o)

Returns `true` if this set contains the specified element.
More formally, returns `true` if and only if this set
contains an element `e` such that `Objects.equals(o, e)`.

##### Parameters

| o | element whose presence in this set is to be tested |
|---|---|

##### Returns

- `true` if this set contains the specified element

#### public boolean
**containsAll**
([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c)

Returns `true` if this set contains all of the elements of the
specified collection. If the specified collection is also a set, this
method returns `true` if it is a *subset* of this set.

##### Parameters

| c | collection to be checked for containment in this set |
|---|---|

##### Returns

- `true` if this set contains all of the elements of the specified collection

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the specified collection is null |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#contains(java.lang.Object)`

#### public boolean
**equals**
([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o)

Compares the specified object with this set for equality.
Returns `true` if the specified object is the same object
as this object, or if it is also a `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html` and the elements
returned by an {@linkplain Set#iterator() iterator} over the
specified set are the same as the elements returned by an
iterator over this set. More formally, the two iterators are
considered to return the same elements if they return the same
number of elements and for every element `e1` returned by
the iterator over the specified set, there is an element
`e2` returned by the iterator over this set such that
`Objects.equals(e1, e2)`.

##### Parameters

| o | object to be compared for equality with this set |
|---|---|

##### Returns

- `true` if the specified object is equal to this set

#### public void
**forEach**
([Consumer](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/function/Consumer.html)\<? super E\> action)

<br />

##### Parameters

| action |   |
|---|---|

#### public boolean
**isEmpty**
()

Returns `true` if this set contains no elements.

##### Returns

- `true` if this set contains no elements

#### public [Iterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Iterator.html)\<E\>
**iterator**
()

Returns an iterator over the elements contained in this set
in the order in which these elements were added.

The returned iterator provides a snapshot of the state of the set
when the iterator was constructed. No synchronization is needed while
traversing the iterator. The iterator does *NOT* support the
`remove` method.

##### Returns

- an iterator over the elements in this set

#### public boolean
**remove**
([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) o)

Removes the specified element from this set if it is present.
More formally, removes an element `e` such that
`Objects.equals(o, e)`, if this set contains such an element.
Returns `true` if this set contained the element (or
equivalently, if this set changed as a result of the call).
(This set will not contain the element once the call returns.)

##### Parameters

| o | object to be removed from this set, if present |
|---|---|

##### Returns

- `true` if this set contained the specified element

#### public boolean
**removeAll**
([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c)

Removes from this set all of its elements that are contained in the
specified collection. If the specified collection is also a set,
this operation effectively modifies this set so that its value is the
*asymmetric set difference* of the two sets.

##### Parameters

| c | collection containing elements to be removed from this set |
|---|---|

##### Returns

- `true` if this set changed as a result of the call

##### Throws

| [ClassCastException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ClassCastException.html) | if the class of an element of this set is incompatible with the specified collection ([optional](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#optional-restrictions)) |
| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if this set contains a null element and the specified collection does not permit null elements ([optional](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#optional-restrictions)), or if the specified collection is null |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#remove(java.lang.Object)`

#### public boolean
**removeIf**
([Predicate](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/function/Predicate.html)\<? super E\> filter)

<br />

##### Parameters

| filter |   |
|---|---|

#### public boolean
**retainAll**
([Collection](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html)\<?\> c)

Retains only the elements in this set that are contained in the
specified collection. In other words, removes from this set all of
its elements that are not contained in the specified collection. If
the specified collection is also a set, this operation effectively
modifies this set so that its value is the *intersection* of the
two sets.

##### Parameters

| c | collection containing elements to be retained in this set |
|---|---|

##### Returns

- `true` if this set changed as a result of the call

##### Throws

| [ClassCastException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ClassCastException.html) | if the class of an element of this set is incompatible with the specified collection ([optional](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#optional-restrictions)) |
| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if this set contains a null element and the specified collection does not permit null elements ([optional](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Collection.html#optional-restrictions)), or if the specified collection is null |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#remove(java.lang.Object)`

#### public int
**size**
()

Returns the number of elements in this set.

##### Returns

- the number of elements in this set

#### public [Spliterator](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html)\<E\>
**spliterator**
()

Returns a `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html` over the elements in this set in the order
in which these elements were added.

The `Spliterator` reports `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html#IMMUTABLE`,
`https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html#DISTINCT`, `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html#SIZED`, and
`https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Spliterator.html#SUBSIZED`.

The spliterator provides a snapshot of the state of the set
when the spliterator was constructed. No synchronization is needed while
operating on the spliterator.

##### Returns

- a `Spliterator` over the elements in this set

#### public T\[\]
**toArray**
(T\[\] a)

Returns an array containing all of the elements in this set; the
runtime type of the returned array is that of the specified array.
If the set fits in the specified array, it is returned therein.
Otherwise, a new array is allocated with the runtime type of the
specified array and the size of this set.

If this set fits in the specified array with room to spare
(i.e., the array has more elements than this set), the element in
the array immediately following the end of the set is set to
`null`. (This is useful in determining the length of this
set *only* if the caller knows that this set does not contain
any null elements.)

If this set makes any guarantees as to what order its elements
are returned by its iterator, this method must return the elements
in the same order.

Like the `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/CopyOnWriteArraySet.html#toArray()` method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may,
under certain circumstances, be used to save allocation costs.

Suppose `x` is a set known to contain only strings.
The following code can be used to dump the set into a newly allocated
array of `String`:

     String[] y = x.toArray(new String[0]);

Note that `toArray(new Object[0])` is identical in function to `toArray()`.

<br />

##### Parameters

| a | the array into which the elements of this set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose. |
|---|---|

##### Returns

- an array containing all the elements in this set

##### Throws

| [ArrayStoreException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ArrayStoreException.html) | if the runtime type of the specified array is not a supertype of the runtime type of every element in this set |
| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the specified array is null |
|---|---|

#### public [Object\[\]](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)
**toArray**
()

Returns an array containing all of the elements in this set.
If this set makes any guarantees as to what order its elements
are returned by its iterator, this method must return the
elements in the same order.

The returned array will be "safe" in that no references to it
are maintained by this set. (In other words, this method must
allocate a new array even if this set is backed by an array).
The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based
APIs.

##### Returns

- an array containing all the elements in this set