Java Program to Implement CopyOnWriteArraySet API Last Updated : 30 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 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 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 Comment More infoAdvertise with us Next Article Java Program to Implement CopyOnWriteArraySet API P pranaythanneru Follow Improve Article Tags : Java Java Programs Java-Collections Java-CopyOnWriteArraySet Practice Tags : JavaJava-Collections Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to 12 min read Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android 4 min read Like