![]() |
VOOZH | about |
In Java, the CopyOnWriteArraySet is the part of the java.util.concurrent package and is used to handle thread-safe operations in multi-threaded environments. It is ideal when the set is frequently read but infrequently modified. The set ensures safe access for multiple threads, as it creates a new copy of the set each time a modification is made, making reads efficient and safe. However, this comes at the cost of slower performance for modifications.
CopyOnWriteArraySet is a class that implements the Set interface. The Set interface extends the Collection interface. The Collection interface extends the Iterable interface and the Object is the root of this hierarchy.
Example 1: This example demonstrates how CopyOnWriteArraySet allows safe concurrent reading and writing by multiple threads without throwing ConcurrentModificationException.
Set: [Geek1, Geek2, Geek3] Added Geeks Reading: Geek1 Reading: Geek2 Reading: Geek3
In Java, the declaration of CopyOnWriteArraySet can be done as:
CopyOnWriteArraySet<Type> setName = new CopyOnWriteArraySet<>();
Constructors | Description |
|---|---|
CopyOnWriteArraySet() | Creates an empty set. |
CopyOnWriteArraySet(Collection c) | Creates a set containing all of the elements of the specified collection. |
Example 2: This example demonstrates how CopyOnWriteArraySet allows thread-safe modification and iteration, where elements can be added by a child thread and safely iterated without causing concurrency issues or exception.
Set after child thread modification: [A, B, C, D] [A, B, C, D] [A, B, C, D] [A, B, C, D] [A, B, D] Final Set: [A, B, D]
Example 3: This example demonstrates how to iterate over a CopyOnWriteArraySet, showing that it allows re-iteration without causing concurrency issues, even after elements are added.
Set contains: GeeksforGeeks Set contains after re-iteration: GeeksforGeeks
Method | Description |
|---|---|
| add(E e) | Adds the specified element to this set if it is not already present. |
| addAll(Collection<? extends E> c) | Adds all of the elements in the specified collection to this set if theyโre not already present. |
| clear() | Removes all of the elements from this set. |
| contains(Object o) | Returns true if this set contains the specified element. |
| containsAll(Collection<?> c) | Returns true if this set contains all of the elements of the specified collection. |
| equals(Object o) | Compares the specified object with this set for equality. |
| forEach(Consumer<? super E> action) | Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
| isEmpty() | Returns true if this set contains no elements. |
| iterator() | Returns an iterator over the elements contained in this set in the order in which these elements were added. |
| remove(Object o) | Removes the specified element from this set if it is present. |
| removeAll(Collection<?> c) | Removes from this set all of its elements that are contained in the specified collection. |
| removeIf(Predicate<? super E> filter) | Removes all of the elements of this collection that satisfy the given predicate. |
| retainAll(Collection<?> c) | Retains only the elements in this set that are contained in the specified collection. |
| size() | Returns the number of elements in this set. |
| spliterator() | Returns a Spliterator over the elements in this set in the order in which these elements were added. |
| toArray() | Returns an array containing all of the elements in this set. |
| 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. |
Property | HashSet | CopyOnWriteArraySet |
|---|---|---|
Package | It belongs to java.util package | It belongs to java.util.concurrent package |
Synchronization | HashSet is not synchronized, meaning itโs not thread-safe. | CopyOnWriteArraySet is synchronized and thread-safe. |
Iterators | Iterators returned my methods iterator() and listiterator() are fail-fast. | Iterators returned are fail-safe |
Added In Version | It was added in JDK 1.2 | It was added in JDK 1.5 |
Performance | It is fast because it does not required synchronzation | It is slower because every modification creates a new copy of the underlying array. |
Exception | It may throw ConcurrentModificationException. If multiple threads modify it while iterating. | It does not throw ConcurrentModificationException because modifications are done on a copy of the internal array. |