APIdia Launch Partner
Top Description Fields Constructors Methods
java.util.concurrent

public Class CopyOnWriteArraySet<E>

extends AbstractSet<E>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Type Parameters
<E>
the type of elements held in this set
Imports
jdk.internal.misc.Unsafe, java.io.IOException, .ObjectInputStream, .ObjectStreamException, .Serial, .StreamCorruptedException, java.util.AbstractSet, .Collection, .Iterator, .Objects, .Set, .Spliterator, .Spliterators, java.util.function.Consumer, .Predicate

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

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();
  }
}

This class is a member of the Java Collections Framework.

Author
Doug Lea
Since
1.5
See Also
CopyOnWriteArrayList

Field Summary

Modifier and TypeField and Description
private final CopyOnWriteArrayList<E>
private static final long

Constructor Summary

AccessConstructor and Description
public
CopyOnWriteArraySet()

Creates an empty set.

public
CopyOnWriteArraySet(Collection<? extends E>
the collection of elements to initially contain
c
)

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

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if this set did not already contain the specified element
add
(E
element to be added to this set
e
)

Overrides java.util.AbstractCollection.add.

Implements java.util.Set.add, java.util.Collection.add.

Adds the specified element to this set if it is not already present.

public boolean

Returns:

true if this set changed as a result of the call
addAll
(Collection<? extends E>
collection containing elements to be added to this set
c
)

Overrides java.util.AbstractCollection.addAll.

Implements java.util.Set.addAll, java.util.Collection.addAll.

Adds all of the elements in the specified collection to this set if they're not already present.

public void
clear()

Overrides java.util.AbstractCollection.clear.

Implements java.util.Set.clear, java.util.Collection.clear.

Removes all of the elements from this set.

private static int

Returns:

-1 if snapshot is not a superset, 0 if the two sets contain precisely the same elements, and 1 if snapshot is a proper superset of the given set
compareSets
(Object[] snapshot, Set<?> set)

Tells whether the objects in snapshot (regarded as a set) are a superset of the given set.

public boolean

Returns:

true if this set contains the specified element
contains
(Object
element whose presence in this set is to be tested
o
)

Overrides java.util.AbstractCollection.contains.

Implements java.util.Set.contains, java.util.Collection.contains.

Returns true if this set contains the specified element.

public boolean

Returns:

true if this set contains all of the elements of the specified collection
containsAll
(Collection<?>
collection to be checked for containment in this set
c
)

Overrides java.util.AbstractCollection.containsAll.

Implements java.util.Set.containsAll, java.util.Collection.containsAll.

Returns true if this set contains all of the elements of the specified collection.

public boolean

Returns:

true if the specified object is equal to this set
equals
(Object
object to be compared for equality with this set
o
)

Overrides java.util.AbstractSet.equals.

Implements java.util.Set.equals, java.util.Collection.equals.

Compares the specified object with this set for equality.

public void
forEach(Consumer<? super E> action)

Overrides default java.lang.Iterable.forEach.

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

public boolean

Returns:

true if this set contains no elements
isEmpty
()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Set.isEmpty, java.util.Collection.isEmpty.

Returns true if this set contains no elements.

public Iterator<E>

Returns:

an iterator over the elements in this set
iterator
()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

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

private void
readObject(ObjectInputStream in)

Reconstitutes the CopyOnWriteArraySet instance from a stream (that is, deserializes it).

private void
readObjectNoData()

De-serialization without data not supported for this class.

public boolean

Returns:

true if this set contained the specified element
remove
(Object
object to be removed from this set, if present
o
)

Overrides java.util.AbstractCollection.remove.

Implements java.util.Set.remove, java.util.Collection.remove.

Removes the specified element from this set if it is present.

public boolean

Returns:

true if this set changed as a result of the call
removeAll
(Collection<?>
collection containing elements to be removed from this set
c
)

Overrides java.util.AbstractSet.removeAll.

Implements java.util.Set.removeAll, java.util.Collection.removeAll.

Removes from this set all of its elements that are contained in the specified collection.

public boolean
removeIf(Predicate<? super E> filter)

Overrides default java.util.Collection.removeIf.

Removes all of the elements of this collection that satisfy the given predicate (optional operation).

public boolean

Returns:

true if this set changed as a result of the call
retainAll
(Collection<?>
collection containing elements to be retained in this set
c
)

Overrides java.util.AbstractCollection.retainAll.

Implements java.util.Set.retainAll, java.util.Collection.retainAll.

Retains only the elements in this set that are contained in the specified collection.

public int

Returns:

the number of elements in this set
size
()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Set.size, java.util.Collection.size.

Returns the number of elements in this set.

public Spliterator<E>

Returns:

a Spliterator over the elements in this set
spliterator
()

Overrides default java.util.Set.spliterator, java.util.Collection.spliterator.

Returns a Spliterator over the elements in this set in the order in which these elements were added.

public Object[]

Returns:

an array containing all the elements in this set
toArray
()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Set.toArray, java.util.Collection.toArray.

Returns an array containing all of the elements in this set.

public <T> T[]

Returns:

an array containing all the elements in this set
toArray
(T[]
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.
a
)

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Set.toArray, java.util.Collection.toArray.

Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.

Inherited from java.util.AbstractSet:
hashCode

Field Detail

alback to summary
private final CopyOnWriteArrayList<E> al
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

CopyOnWriteArraySetback to summary
public CopyOnWriteArraySet()

Creates an empty set.

CopyOnWriteArraySetback to summary
public CopyOnWriteArraySet(Collection<? extends E> c)

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

Parameters
c:Collection<? extends E>

the collection of elements to initially contain

Exceptions
NullPointerException:
if the specified collection is null

Method Detail

addback to summary
public boolean add(E e)

Overrides java.util.AbstractCollection.add.

Implements java.util.Set.add, java.util.Collection.add.

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:E

element to be added to this set

Returns:boolean

true if this set did not already contain the specified element

addAllback to summary
public boolean addAll(Collection<? extends E> c)

Overrides java.util.AbstractCollection.addAll.

Implements java.util.Set.addAll, java.util.Collection.addAll.

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<? extends E>

collection containing elements to be added to this set

Returns:boolean

true if this set changed as a result of the call

Exceptions
NullPointerException:
if the specified collection is null
See Also
add(Object)
clearback to summary
public void clear()

Overrides java.util.AbstractCollection.clear.

Implements java.util.Set.clear, java.util.Collection.clear.

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

compareSetsback to summary
private static int compareSets(Object[] snapshot, Set<?> set)

Tells whether the objects in snapshot (regarded as a set) are a superset of the given set.

Returns:int

-1 if snapshot is not a superset, 0 if the two sets contain precisely the same elements, and 1 if snapshot is a proper superset of the given set

containsback to summary
public boolean contains(Object o)

Overrides java.util.AbstractCollection.contains.

Implements java.util.Set.contains, java.util.Collection.contains.

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:Object

element whose presence in this set is to be tested

Returns:boolean

true if this set contains the specified element

containsAllback to summary
public boolean containsAll(Collection<?> c)

Overrides java.util.AbstractCollection.containsAll.

Implements java.util.Set.containsAll, java.util.Collection.containsAll.

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<?>

collection to be checked for containment in this set

Returns:boolean

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

Exceptions
NullPointerException:
if the specified collection is null
See Also
contains(Object)
equalsback to summary
public boolean equals(Object o)

Overrides java.util.AbstractSet.equals.

Implements java.util.Set.equals, java.util.Collection.equals.

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 Set and the elements returned by an 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

object to be compared for equality with this set

Returns:boolean

true if the specified object is equal to this set

forEachback to summary
public void forEach(Consumer<? super E> action)

Overrides default java.lang.Iterable.forEach.

Doc from java.lang.Iterable.forEach.

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller.

The behavior of this method is unspecified if the action performs side-effects that modify the underlying source of elements, unless an overriding class has specified a concurrent modification policy.

Exceptions
NullPointerException:
if the specified action is null
isEmptyback to summary
public boolean isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Set.isEmpty, java.util.Collection.isEmpty.

Returns true if this set contains no elements.

Returns:boolean

true if this set contains no elements

iteratorback to summary
public Iterator<E> iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Set.iterator, java.util.Collection.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:Iterator<E>

an iterator over the elements in this set

readObjectback to summary
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException

Reconstitutes the CopyOnWriteArraySet instance from a stream (that is, deserializes it).

Annotations
@Serial
Exceptions
StreamCorruptedException:
if the object read from the stream is invalid.
readObjectNoDataback to summary
private void readObjectNoData() throws ObjectStreamException

De-serialization without data not supported for this class.

Annotations
@Serial
removeback to summary
public boolean remove(Object o)

Overrides java.util.AbstractCollection.remove.

Implements java.util.Set.remove, java.util.Collection.remove.

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

object to be removed from this set, if present

Returns:boolean

true if this set contained the specified element

removeAllback to summary
public boolean removeAll(Collection<?> c)

Overrides java.util.AbstractSet.removeAll.

Implements java.util.Set.removeAll, java.util.Collection.removeAll.

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<?>

collection containing elements to be removed from this set

Returns:boolean

true if this set changed as a result of the call

Exceptions
ClassCastException:
if the class of an element of this set is incompatible with the specified collection (optional)
NullPointerException:
if this set contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null
See Also
remove(Object)
removeIfback to summary
public boolean removeIf(Predicate<? super E> filter)

Overrides default java.util.Collection.removeIf.

Doc from java.util.Collection.removeIf.

Removes all of the elements of this collection that satisfy the given predicate (optional operation). Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.

Exceptions
NullPointerException:
if the specified filter is null
retainAllback to summary
public boolean retainAll(Collection<?> c)

Overrides java.util.AbstractCollection.retainAll.

Implements java.util.Set.retainAll, java.util.Collection.retainAll.

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<?>

collection containing elements to be retained in this set

Returns:boolean

true if this set changed as a result of the call

Exceptions
ClassCastException:
if the class of an element of this set is incompatible with the specified collection (optional)
NullPointerException:
if this set contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null
See Also
remove(Object)
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Set.size, java.util.Collection.size.

Returns the number of elements in this set.

Returns:int

the number of elements in this set

spliteratorback to summary
public Spliterator<E> spliterator()

Overrides default java.util.Set.spliterator, java.util.Collection.spliterator.

Returns a Spliterator over the elements in this set in the order in which these elements were added.

The Spliterator reports Spliterator#IMMUTABLE, Spliterator#DISTINCT, Spliterator#SIZED, and Spliterator#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:Spliterator<E>

a Spliterator over the elements in this set

Since
1.8
toArrayback to summary
public Object[] toArray()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Set.toArray, java.util.Collection.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:Object[]

an array containing all the elements in this set

toArrayback to summary
public <T> T[] toArray(T[] a)

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Set.toArray, java.util.Collection.toArray.

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 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().
Parameters
a:T[]

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:T[]

an array containing all the elements in this set

Exceptions
ArrayStoreException:
if the runtime type of the specified array is not a supertype of the runtime type of every element in this set
NullPointerException:
if the specified array is null