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.

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
 
public class CopyOnWriteArraySetImpl<E>
{
    private CopyOnWriteArraySet<E> copyOnWriteArraySet;
 
    /** Creates an empty set. **/
    public CopyOnWriteArraySetImpl()
    {
        copyOnWriteArraySet = new CopyOnWriteArraySet<E>();
    }
 
    /**
     * Creates a set containing all of the elements of the specified collection.
    **/
    public CopyOnWriteArraySetImpl(Collection<? extends E> c)
    {
        copyOnWriteArraySet = new CopyOnWriteArraySet<E>(c);
    }
 
    /** adds the specified element if not already present **/
    public boolean add(E eobj)
    {
        return copyOnWriteArraySet.add(eobj);
    }
 
    /** return true if this set contains the specified element **/
    public boolean contains(Object obj)
    {
        return copyOnWriteArraySet.contains(obj);
    }
 
    /** returns true if the set is empty **/
    public boolean isEmpty()
    {
        return copyOnWriteArraySet.isEmpty();
    }
 
    /** returns an iterator over the elements in the set **/
    public Iterator<E> iterator()
    {
        return copyOnWriteArraySet.iterator();
    }
 
    /** removes the specified element from this set if present **/
    public boolean remove(Object obj)
    {
        return copyOnWriteArraySet.remove(obj);
    }
 
    /** returns the number of elements in set **/
    public int size()
    {
        return copyOnWriteArraySet.size();
    }
 
    /** removes all elements from this set **/
    public void clear()
    {
        copyOnWriteArraySet.clear();
    }
 
    /** Returns an array containing all of the elements in this set. **/
    public Object[] toArray()
    {
        return copyOnWriteArraySet.toArray();
    }
 
    /**
     * Adds 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);
    }
 
    /**
     * Retains 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);
    }
 
    /**
     * Removes from this set all of its 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; the runtime
     * type of the returned array is that of the specified array
    **/
    public <T> T[] toArray(T[] a) throws ArrayStoreException, NullPointerException
    {
        return copyOnWriteArraySet.toArray(a);
    }
 
    public static void main(String... arg)
    {
        CopyOnWriteArraySetImpl<Integer> copyOnWriteArraySet = new CopyOnWriteArraySetImpl<Integer>();
        if (copyOnWriteArraySet.add(10))
            System.out.println("element 10 added");
        if (copyOnWriteArraySet.add(20))
            System.out.println("element 20 added");
        if (copyOnWriteArraySet.add(30))
            System.out.println("element 30 added");
        System.out.println("the size of copyOnWriteArraySet is " + copyOnWriteArraySet.size());
        if (copyOnWriteArraySet.contains(40))
            System.out.println("copyOnWriteArraySet contains 40");
        else
            System.out.println("copyOnWriteArraySet does not contain 40");
        if (copyOnWriteArraySet.remove(20))
            System.out.println("element 20 removed");
        else
            System.out.println("element 20 not removed");
        System.out.println("the element of copyOnWriteArraySet are");
        Iterator<Integer> iterator = copyOnWriteArraySet.iterator();
        while (iterator.hasNext())
        {
            System.out.print(iterator.next() + "\t");
        }
        System.out.println();
        Set<Integer> removedSet = new HashSet<Integer>();
        removedSet.add(10);
        removedSet.add(20);
        System.out.println("the elements after removing");
        copyOnWriteArraySet.removeAll(removedSet);
        Iterator<Integer> riterator = copyOnWriteArraySet.iterator();
        while (riterator.hasNext()) 
        {
            System.out.print(riterator.next() + "\t");
        }
        System.out.println();
        copyOnWriteArraySet.clear();
        System.out.println("copyOnWriteArraySet cleared");
        if (copyOnWriteArraySet.isEmpty())
            System.out.println("copyOnWriteArraySet is empty");
        else
            System.out.println("copyOnWriteArraySet is not empty");
    }
}
$ 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

Related posts:

Java Program to Describe the Representation of Graph using Adjacency List
Java Program to Implement Circular Singly Linked List
Guide to the ConcurrentSkipListMap
Guide to @ConfigurationProperties in Spring Boot
Spring Data – CrudRepository save() Method
Test a REST API with Java
Server-Sent Events in Spring
Java Program to Solve Knapsack Problem Using Dynamic Programming
Tính trừu tượng (Abstraction) trong Java
Guide to the Volatile Keyword in Java
Java Program to Find a Good Feedback Edge Set in a Graph
A Guide to Java HashMap
Giới thiệu SOAP UI và thực hiện test Web Service
Validate email address exists or not by Java Code
Java Program to implement Bi Directional Map
Apache Commons Collections SetUtils
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Java Program to Create a Random Linear Extension for a DAG
Java Program to Generate Random Hexadecimal Byte
Lớp TreeMap trong Java
How to Find an Element in a List with Java
Java Program to Implement the String Search Algorithm for Short Text Sizes
Java 8 – Powerful Comparison with Lambdas
Java Program to implement Dynamic Array
Java Program to Implement Binary Heap
Spring Boot - Code Structure
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Implement Hopcroft Algorithm
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Rate Limiting in Spring Cloud Netflix Zuul
Introduction to Spring Data JPA
Giới thiệu Swagger – Công cụ document cho RESTfull APIs