CopyOnWriteArraySet in java
CopyOnWriteArraySet is a Set that uses an internal CopyOnWriteArrayList for all of its operations. It is introduced in JDK 1.5, we can say that it is a thread-safe version of Set.
It shares some properties of Set and also has its properties:
- The internal implementation of CopyOnWriteArraySet is CopyOnWriteArrayList only.
- Multiple Threads can perform update operation simultaneously but for every update operation, a separate cloned copy is created. As for every update a new cloned copy will be created which is costly. Hence if multiple update operation is required then it is not recommended to use CopyOnWriteArraySet.
- While one thread iterating the Set, other threads can perform updation, here we won't get any runtime exception like ConcurrentModificationException.
- An iterator of CopyOnWriteArraySet class can perform only read-only and won't perform the deletion, otherwise, we will get Run-time exception UnsupportedOperationException.
Constructors of CopyOnWriteArraySet:
- CopyOnWriteArraySet c = new CopyOnWriteArraySet(): Creates an empty set.
- CopyOnWriteArraySet c = new CopyOnWriteArraySet(Collection c): Creates a set containing all of the elements of the specified collection.
Methods in CopyOnWriteArraySet:
- add? (E e): This method Adds the specified element to this set if it is not already present.
- addAll?(Collection c): This method Adds all of the elements in the specified collection to this set if they’re not already present.
- clear? (): This method Removes all of the elements from this set.
- contains? (Object o): This method returns true if this set contains the specified element.
- contains? (Collection c): This method returns true if this set contains all of the elements of the specified collection.
- equals? (Object o): This method Compares the specified object with this set for equality.
- forEach?(Consumer action): This method Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
- isEmpty?(): This method returns true if this set contains no elements.
- iterator? (): This method returns an iterator over the elements contained in this set in the order in which these elements were added.
- remove? (Object o): This method Removes the specified element from this set if it is present.
- removeAll?(Collection c): This method Removes from this set all of its elements that are contained in the specified collection.
- remove? (Predicate filter): This method Removes all of the elements of this collection that satisfy the given predicate.
- retain? (Collection c): This method Retains only the elements in this set that are contained in the specified collection.
- size?(): This method returns the number of elements in this set.
- spliterator? (): This method returns a Spliterator over the elements in this set in the order in which these elements were added.
- toArray?(): This method returns an array containing all of the elements in this set.
- toArray?(T[] a): This method returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.
Example:
filter_none
edit
play_arrow
brightness_4
|
Output:
[A, B, C, D] A B C Exception in thread "main" java.lang.UnsupportedOperationException
post a comment