Java Program to Implement CopyOnWriteArraySet API

This Java program Implements CopyOnWriteArraySet API.A Set which is best suited for applications in which set sizes generally stay small, read-only operations vastly outnumber mutative operations, and you need to prevent interference among threads during traversal.It is thread-safe.Mutative operations (add, set, remove, etc.) are expensive since they usually entail copying the entire underlying array.Iterators do not support the mutative remove operation.Traversal via iterators is fast and cannot encounter interference from other threads. Iterators rely on unchanging snapshots of the array at the time the iterators were constructed.

Here is the source code of the Java Program to Implement CopyOnWriteArraySet API.The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Collection;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. import java.util.Set;
  5. import java.util.concurrent.CopyOnWriteArraySet;
  6.  
  7. public class CopyOnWriteArraySetImpl<E>
  8. {
  9.     private CopyOnWriteArraySet<E> copyOnWriteArraySet;
  10.  
  11.     /** Creates an empty set. **/
  12.     public CopyOnWriteArraySetImpl()
  13.     {
  14.         copyOnWriteArraySet = new CopyOnWriteArraySet<E>();
  15.     }
  16.  
  17.     /**
  18.      * Creates a set containing all of the elements of the specified collection.
  19.     **/
  20.     public CopyOnWriteArraySetImpl(Collection<? extends E> c)
  21.     {
  22.         copyOnWriteArraySet = new CopyOnWriteArraySet<E>(c);
  23.     }
  24.  
  25.     /** adds the specified element if not already present **/
  26.     public boolean add(E eobj)
  27.     {
  28.         return copyOnWriteArraySet.add(eobj);
  29.     }
  30.  
  31.     /** return true if this set contains the specified element **/
  32.     public boolean contains(Object obj)
  33.     {
  34.         return copyOnWriteArraySet.contains(obj);
  35.     }
  36.  
  37.     /** returns true if the set is empty **/
  38.     public boolean isEmpty()
  39.     {
  40.         return copyOnWriteArraySet.isEmpty();
  41.     }
  42.  
  43.     /** returns an iterator over the elements in the set **/
  44.     public Iterator<E> iterator()
  45.     {
  46.         return copyOnWriteArraySet.iterator();
  47.     }
  48.  
  49.     /** removes the specified element from this set if present **/
  50.     public boolean remove(Object obj)
  51.     {
  52.         return copyOnWriteArraySet.remove(obj);
  53.     }
  54.  
  55.     /** returns the number of elements in set **/
  56.     public int size()
  57.     {
  58.         return copyOnWriteArraySet.size();
  59.     }
  60.  
  61.     /** removes all elements from this set **/
  62.     public void clear()
  63.     {
  64.         copyOnWriteArraySet.clear();
  65.     }
  66.  
  67.     /** Returns an array containing all of the elements in this set. **/
  68.     public Object[] toArray()
  69.     {
  70.         return copyOnWriteArraySet.toArray();
  71.     }
  72.  
  73.     /**
  74.      * Adds all of the elements in the specified collection to this set if
  75.      * they're not already present
  76.     **/
  77.     public boolean addAll(Collection<? extends E> c)
  78.     throws UnsupportedOperationException, ClassCastException, NullPointerException, IllegalArgumentException 
  79.     {
  80.         return copyOnWriteArraySet.addAll(c);
  81.     }
  82.  
  83.     /**
  84.      * Retains only the elements in this set that are contained in the specified
  85.      * collection
  86.     **/
  87.     public boolean retainAll(Collection<?> c)
  88.       throws UnsupportedOperationException, ClassCastException, NullPointerException
  89.     {
  90.         return copyOnWriteArraySet.retainAll(c);
  91.     }
  92.  
  93.     /**
  94.      * Removes from this set all of its elements that are contained in the
  95.      * specified collection
  96.     **/
  97.     public boolean removeAll(Collection<?> c)
  98.       throws UnsupportedOperationException, NullPointerException, ClassCastException
  99.     {
  100.         return copyOnWriteArraySet.retainAll(c);
  101.     }
  102.  
  103.     /**
  104.      * Returns an array containing all of the elements in this set; the runtime
  105.      * type of the returned array is that of the specified array
  106.     **/
  107.     public <T> T[] toArray(T[] a) throws ArrayStoreException, NullPointerException
  108.     {
  109.         return copyOnWriteArraySet.toArray(a);
  110.     }
  111.  
  112.     public static void main(String... arg)
  113.     {
  114.         CopyOnWriteArraySetImpl<Integer> copyOnWriteArraySet = new CopyOnWriteArraySetImpl<Integer>();
  115.         if (copyOnWriteArraySet.add(10))
  116.             System.out.println("element 10 added");
  117.         if (copyOnWriteArraySet.add(20))
  118.             System.out.println("element 20 added");
  119.         if (copyOnWriteArraySet.add(30))
  120.             System.out.println("element 30 added");
  121.         System.out.println("the size of copyOnWriteArraySet is " + copyOnWriteArraySet.size());
  122.         if (copyOnWriteArraySet.contains(40))
  123.             System.out.println("copyOnWriteArraySet contains 40");
  124.         else
  125.             System.out.println("copyOnWriteArraySet does not contain 40");
  126.         if (copyOnWriteArraySet.remove(20))
  127.             System.out.println("element 20 removed");
  128.         else
  129.             System.out.println("element 20 not removed");
  130.         System.out.println("the element of copyOnWriteArraySet are");
  131.         Iterator<Integer> iterator = copyOnWriteArraySet.iterator();
  132.         while (iterator.hasNext())
  133.         {
  134.             System.out.print(iterator.next() + "\t");
  135.         }
  136.         System.out.println();
  137.         Set<Integer> removedSet = new HashSet<Integer>();
  138.         removedSet.add(10);
  139.         removedSet.add(20);
  140.         System.out.println("the elements after removing");
  141.         copyOnWriteArraySet.removeAll(removedSet);
  142.         Iterator<Integer> riterator = copyOnWriteArraySet.iterator();
  143.         while (riterator.hasNext()) 
  144.         {
  145.             System.out.print(riterator.next() + "\t");
  146.         }
  147.         System.out.println();
  148.         copyOnWriteArraySet.clear();
  149.         System.out.println("copyOnWriteArraySet cleared");
  150.         if (copyOnWriteArraySet.isEmpty())
  151.             System.out.println("copyOnWriteArraySet is empty");
  152.         else
  153.             System.out.println("copyOnWriteArraySet is not empty");
  154.     }
  155. }

$ javac CopyOnWriteArraySetImpl.java
$ java CopyOnWriteArraySetImpl
element 10 added
element 20 added
element 30 added
the size of copyOnWriteArraySet is 3
copyOnWriteArraySet does not contain 40
element 20 removed
the element of copyOnWriteArraySet are
10	30	
the elements after removing
10	
copyOnWriteArraySet cleared
copyOnWriteArraySet is empty

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.