Java Program to Implement CopyOnWriteArraySet API

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.
Implementation:
Example
Java
// Java Program to Implement CopyOnWriteArraySet APIÂ
// Importing required utility classes//Â from java.util packageimport java.util.Collection;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import java.util.concurrent.CopyOnWriteArraySet;Â
// CopyOnWriteArraySetImplpublic class<E> GFG {Â Â Â Â private CopyOnWriteArraySet<E> copyOnWriteArraySet;Â
    // Constructor of 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 specified 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,               IllegalArgumentException    {        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        System.out.println(            "The size of copyOnWriteArraySet is "            + copyOnWriteArraySet.size());Â
        // Checking whether the Set contains element        // using the contains() method        if (copyOnWriteArraySet.contains(14))            System.out.println(                "The copyOnWriteArraySet contains 14");        elseÂ
            System.out.println(                "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");        elseÂ
            // Print desired element is not removed            System.out.println("Element 13 not removed");Â
        // Now, print and display the elements        System.out.println(            "The element of copyOnWriteArraySet are");        Iterator<Integer> iterator            = copyOnWriteArraySet.iterator();Â
        // Condition holds true till there is        // single element remaining        while (iterator.hasNext()) {Â
            // Print and display all elements size            System.out.print(iterator.next() + "\t");        }Â
        // Appending a new line for better readability        System.out.println();Â
        // Creating an object of Set class of integer type        Set<Integer> removedSet = new HashSet<Integer>();Â
        // Custom input entries to above Set        removedSet.add(12);        removedSet.add(13);Â
        // Display message only        System.out.println("The elements after removing");Â
        // removeAll() method wipes off all elements        // that was present in Set object        copyOnWriteArraySet.removeAll(removedSet);Â
        // Iterator to traverse the elements        Iterator<Integer> riterator            = copyOnWriteArraySet.iterator();Â
        // Again condition holds true till there is        // single element remaining in the List        while (riterator.hasNext()) {Â
            // Printing the elements in the object            // using the next() method            System.out.print(riterator.next() + "\t");        }Â
        // New line        System.out.println();Â
        // Removing all elements from the Set using clear()        // method        copyOnWriteArraySet.clear();Â
        // Display message to showcase all elements are        // removed        System.out.println(            "copyOnWriteArraySet Elements are completely removed");Â
        // Lastly, verifying whether the Set is empty or not        if (copyOnWriteArraySet.isEmpty())Â
            // Print statement if no elements in Set            System.out.println(                "copyOnWriteArraySet is empty");        elseÂ
            // Print statement if elements found in Set            System.out.println(                "copyOnWriteArraySet is not empty");    }} |
Output:
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 12 copyOnWriteArraySet Elements are completely removed copyOnWriteArraySet is empty




