Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Implement CopyOnWriteArraySet API

  • Last Updated : 15 Jul, 2021

CopyOnWriteArraySet is a member of the Java Collections Framework. It is a Set that uses an internal CopyOnWriteArrayList for all of its operations. It was introduced in JDK 1.5, we can say that it is a thread-safe version of Set. To use this class, we need to import it from java.util.concurrent package.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




// Java Program to Implement CopyOnWriteArraySet API
// Importing required utility classes
//  from java.util package
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
// CopyOnWriteArraySetImpl
public class<E> GFG {
    private CopyOnWriteArraySet<E> copyOnWriteArraySet;
    // Constructor oof this class
    public GFG()
        // Creating an empty set
        copyOnWriteArraySet = new CopyOnWriteArraySet<E>();
    // Creates a set containing all of the elements
    // of the specified collection
    public GFG(Collection<? extends E> c)
        copyOnWriteArraySet = new CopyOnWriteArraySet<E>(c);
    // Method
    // To add the specified element into list
    // if not already present
    public boolean add(E eobj)
        return copyOnWriteArraySet.add(eobj);
    // Method
    // Returning true if there is a specified element
    // present in the list
    public boolean contains(Object obj)
        return copyOnWriteArraySet.contains(obj);
    // Method
    // Returning true if the set is empty
    public boolean isEmpty()
        return copyOnWriteArraySet.isEmpty();
    // Method
    // To traverse over the elements
    // present in the set
    public Iterator<E> iterator()
        return copyOnWriteArraySet.iterator();
    // Method
    // To remove the secified element
    // present in the Set
    public boolean remove(Object obj)
        return copyOnWriteArraySet.remove(obj);
    // Method
    // Returning the number of elements
    // present in the set
    public int size() { return copyOnWriteArraySet.size(); }
    // Method
    // Removing all elements from the given set
    public void clear() { copyOnWriteArraySet.clear(); }
    // Method
    // Returning an array containing all of the elements
    // present in this set
    public Object[] toArray()
        return copyOnWriteArraySet.toArray();
    // Method
    // Now, adding all of the elements in the specified
    // collection to this set if they're not already present
    public boolean addAll(Collection<? extends E> c)
        throws UnsupportedOperationException,
               ClassCastException, NullPointerException,
        return copyOnWriteArraySet.addAll(c);
    // Method
    // Returns only the elements in this set that are
    // contained in the specified collection
    public boolean retainAll(Collection<?> c)
        throws UnsupportedOperationException,
               ClassCastException, NullPointerException
        return copyOnWriteArraySet.retainAll(c);
    // Method
    // Removes from this set the elements that are contained
    // in the specified collection
    public boolean removeAll(Collection<?> c)
        throws UnsupportedOperationException,
               NullPointerException, ClassCastException
        return copyOnWriteArraySet.retainAll(c);
    // Returns an array containing all of the elements in
    // this set.
    public <T> T[] toArray(T[] a)
        throws ArrayStoreException, NullPointerException
        return copyOnWriteArraySet.toArray(a);
    // Method
    // Main driver Method
    public static void main(String args[])
        // Creating an object of above class (GFG class)
        // Declaring object of integer type
        GFG<Integer> copyOnWriteArraySet
            = new GFG<Integer>();
        // Adding custom input elements after condition
        // check
        // Custom input elements are added
        // using the add() method
        if (copyOnWriteArraySet.add(12))
            System.out.println("Element 12 added to Set");
        if (copyOnWriteArraySet.add(13))
            System.out.println("Element 13 added to Set");
        if (copyOnWriteArraySet.add(14))
            System.out.println("Element 14 added to Set");
        if (copyOnWriteArraySet.add(15))
            System.out.println("Element 15 added to Set");
        // Print and display the current size of Set
            "The size of copyOnWriteArraySet is "
            + copyOnWriteArraySet.size());
        // Checking whrtjer the Set contains element
        //  using the contains() method
        if (copyOnWriteArraySet.contains(14))
                "The copyOnWriteArraySet contains 14");
                "The copyOnWriteArraySet does not contain 14");
        // Removing element from the Set
        // using remove() method
        if (copyOnWriteArraySet.remove(13))
            // Print desired element is removed
            System.out.println("Element 13 removed");
            // Print desired element is not removed
            System.out.println("Element 13 not removed");
        // Now, print and display the elements
            "The element of copyOnWriteArraySet are");
        Iterator<Integer> iterator
            = copyOnWriteArraySet.iterator();
        // Condition holds true til there is
        // single element remaining
        while (iterator.hasNext()) {
            // Print and display all elements size
            System.out.print( + "\t");
        // Appendind a new line for better readability
        // Creating an object of Set class of integer type
        Set<Integer> removedSet = new HashSet<Integer>();
        // Custom input entries to above Set
        // Display message only
        System.out.println("The elements after removing");
        // removeAll()  method wipes off all elements
        // that was present in Set object
        // Iterator to traverse the elements
        Iterator<Integer> riterator
            = copyOnWriteArraySet.iterator();
        // Again condition holds true till there is
        // single elemennt remaining in the List
        while (riterator.hasNext()) {
            // Printing the elements in the object
            // using the next() method
            System.out.print( + "\t");
        // New line
        // Removing all elements from the Set using clear()
        // method
        // Display message to showcase all elements are
        // removed
            "copyOnWriteArraySet Elements are completely removed");
        // Lastly, verifying whether the Set is empty or not
        if (copyOnWriteArraySet.isEmpty())
            // Print statement if no elements in Set
                "copyOnWriteArraySet is empty");
            // Print statement if elements found in Set
                "copyOnWriteArraySet is not empty");


Element 12 added to Set
Element 13 added to Set
Element 14 added to Set
Element 15 added to Set
The size of copyOnWriteArraySet is 4
The copyOnWriteArraySet contains 14
Element 13 removed
The element of copyOnWriteArraySet are
12    14    15    
The elements after removing
copyOnWriteArraySet Elements are completely removed
copyOnWriteArraySet is empty


My Personal Notes arrow_drop_up
Recommended Articles
Page :