The CopyOnWriteArraySet class implements the Serilizable, Iterable
Java Collections Framework- CopyOnWriteArraySet class
The CopyOnWriteArraySet class implements the Serilizable, Iterable<E>, Collection<E>, Set<E> interfaces. For all of the CopyOnWriteArraySet operations a Set uses an inner CopyOnWriteArrayList. This class is best suited for applications where the set contains only a few elements and where read-only operations greatly outnumber writes and there is a need to you to prevent the interference among threads during traversal. This class is a Thread-safe. In this class Iterator traverses fast and cannot be obstructed from the other threads.
This class is a member of Java Collection Framework.
The CopyOnWriteArraySet class have two constructor.
CopyOnWriteArraySet() : This constructor makes an empty set.
CopyOnWriteArraySet(Collection c) : This constructor makes a set that keeps all of the elements of a particular collection.
Syntax
public class CopyOnWriteArraySet<E>
Parameter description
E : It is the type of element that is held by this collection.
Methods of CopyOnWriteArraySet are :
- boolean add(E e)
- boolean addAll(Collection<? extends E> c)
- void clear()
- boolean contains(Object o)
- boolean containsAll(Collection<?> c)
- boolean equals(Object o)
- boolean isEmpty()
- Iterator<E> iterator()
- boolean remove(Object o)
- boolean removeAll(Collection<?> c)
- boolean retainAll(Collection<?> c)
- int size()
- Object[] toArray()
- <T> T[] toArray(T[] a)
Example :
Here we will discuss an example in which some methods of this class are implemented.
package devmanuals.com;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.*;
import java.util.Set;
import java.util.Iterator;
public class CopyOnWriteArraySetDemo {
public static void main(String args[]) {
CopyOnWriteArraySet set = new CopyOnWriteArraySet();
set.add("A");
set.add("B");
set.add("C");
set.add("D");
set.add("E");
set.add("F");
System.out.println("Elements of set = " + set);
Set s = new ConcurrentSkipListSet();
s.add(1);
s.add(2);
s.add(3);
s.add(4);
s.add(5);
s.add(6);
// addAll() method
boolean bol = set.addAll(s);
System.out.println("Elements after adding all the elements into one set = "+ set);
// contains() method
boolean bol1 = s.contains(6);
System.out.println("Does set 's' contains '6' : " + bol1);
// containsAll() method
boolean bol2 = set.containsAll(s);
System.out.println("Does Set 'set' contains the elements of Set 's' : "+ bol2);
// equals() method
boolean bol3 = set.equals(s);
System.out.println("Are the elements of Set 's' and 'set' equals : "+ bol3);
// isEmpty() method
boolean bol4 = set.isEmpty();
System.out.println("Set 'set' is empty : " + bol4);
// iterator() method
Iterator itr = set.iterator();
System.out.println("Elements of set =");
while (itr.hasNext()) {
System.out.println(itr.next());
}
// size() method
Integer it = set.size();
System.out.println("Size of set = " + it);
// remove(0) method
boolean bol5 = s.remove(4);
System.out.println("Did the element remove from set 's' : " + bol5);
// clear() method
s.clear();
System.out.println("Elements of the set 's' : " + s);
}
}
Output :
|
Elements of set = [A, B, C, D, E, F] Elements after adding all the elements into one set = [A, B, C,D, E, F, 1, 2, 3, 4, 5, 6] Does set 's' contains '6' : true Does Set 'set' contains the elements of Set 's' : true Are the elements of Set 's' and 'set' equals : false Set 'set' is empty : false Elements of set = A B C D E F 1 2 3 4 5 6 Size of set = 12 Did the element remove from set 's' : true Elements of the set 's' : [] |

[ 0 ] Comments