Set that uses an internal CopyOnWriteArrayList
for all of its operations. Thus, it shares the same basic properties:
add, set, remove, etc.)
are expensive since they usually entail copying the entire underlying
array.
remove operation.
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.
CopyOnWriteArrayList
| Modifier and Type | Field and Description |
|---|---|
| private final CopyOnWriteArrayList | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| public | |
| 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. |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: true if this set did not already contain the specified
elementelement to be added to this set e)Overrides java. Implements java. 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 callcollection containing elements to be added to this set c)Overrides java. Implements java. Adds all of the elements in the specified collection to this set if they're not already present. |
| public void | clear()
Overrides java. Implements java. 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 setTells 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 elementelement whose presence in this set is to be tested o)Overrides java. Implements java. Returns |
| public boolean | Returns: true if this set contains all of the elements of the
specified collectioncollection to be checked for containment in this set c)Overrides java. Implements java. Returns |
| public boolean | Returns: true if the specified object is equal to this setobject to be compared for equality with this set o)Overrides java. Implements java. Compares the specified object with this set for equality. |
| public void | forEach(Consumer<? super E> action)
Overrides default java. Performs the given action for each element of the |
| public boolean | Returns: true if this set contains no elementsOverrides java. Implements java. Returns |
| public Iterator | Returns: an iterator over the elements in this setImplements abstract java. Implements java. 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 |
| private void | |
| public boolean | Returns: true if this set contained the specified elementobject to be removed from this set, if present o)Overrides java. Implements java. Removes the specified element from this set if it is present. |
| public boolean | Returns: true if this set changed as a result of the callcollection containing elements to be removed from this set c)Overrides java. Implements java. 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. 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 callcollection containing elements to be retained in this set c)Overrides java. Implements java. Retains only the elements in this set that are contained in the specified collection. |
| public int | Returns: the number of elements in this setImplements abstract java. Implements java. Returns the number of elements in this set. |
| public Spliterator | Returns: aSpliterator over the elements in this setOverrides default java. Returns a |
| public Object[] | Returns: an array containing all the elements in this setOverrides java. Implements java. Returns an array containing all of the elements in this set. |
| public <T> T[] | Returns: an array containing all the elements in this setthe 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. Implements java. Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. |
| al | back to summary |
|---|---|
| private final CopyOnWriteArrayList<E> al | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| CopyOnWriteArraySet | back to summary |
|---|---|
| public CopyOnWriteArraySet() Creates an empty set. | |
| CopyOnWriteArraySet | back to summary |
|---|---|
| public CopyOnWriteArraySet(Collection<? extends E> c) Creates a set containing all of the elements of the specified collection.
| |
| add | back to summary |
|---|---|
| public boolean add(E e) Overrides java. Implements java. Adds the specified element to this set if it is not already present.
More formally, adds the specified element
| |
| addAll | back to summary |
|---|---|
| public boolean addAll(Collection<? extends E> c) Overrides java. Implements java. 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
| |
| clear | back to summary |
|---|---|
| public void clear() Overrides java. Implements java. Removes all of the elements from this set. The set will be empty after this call returns. | |
| compareSets | back 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.
| |
| contains | back to summary |
|---|---|
| public boolean contains(Object o) Overrides java. Implements java. Returns
| |
| containsAll | back to summary |
|---|---|
| public boolean containsAll(Collection<?> c) Overrides java. Implements java. Returns
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object o) Overrides java. Implements java. Compares the specified object with this set for equality.
Returns
| |
| forEach | back to summary |
|---|---|
| public void forEach(Consumer<? super E> action) Overrides default java. Doc from java. Performs the given action for each element of the 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.
| |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Overrides java. Implements java. Returns
| |
| iterator | back to summary |
|---|---|
| public Iterator Implements abstract java. Implements java. 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
| |
| readObject | back to summary |
|---|---|
| private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException Reconstitutes the
| |
| readObjectNoData | back to summary |
|---|---|
| private void readObjectNoData() throws ObjectStreamException De-serialization without data not supported for this class.
| |
| remove | back to summary |
|---|---|
| public boolean remove(Object o) Overrides java. Implements java. Removes the specified element from this set if it is present.
More formally, removes an element
| |
| removeAll | back to summary |
|---|---|
| public boolean removeAll(Collection<?> c) Overrides java. Implements java. 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.
| |
| removeIf | back to summary |
|---|---|
| public boolean removeIf(Predicate<? super E> filter) Overrides default java. Doc from java. 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.
| |
| retainAll | back to summary |
|---|---|
| public boolean retainAll(Collection<?> c) Overrides java. Implements java. 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.
| |
| size | back to summary |
|---|---|
| public int size() Implements abstract java. Implements java. Returns the number of elements in this set.
| |
| spliterator | back to summary |
|---|---|
| public Spliterator Overrides default java. Returns a The 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.
| |
| toArray | back to summary |
|---|---|
| public Object[] toArray() Overrides java. Implements java. 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.
| |
| toArray | back to summary |
|---|---|
| public <T> T[] toArray(T[] a) Overrides java. Implements java. 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
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 Suppose
Note that toArray(new Object[0]) is identical in function to
toArray().
| |