Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

CopyOnWriteArraySet clear() method in Java

  • Last Updated : 26 Nov, 2018

The clear() method of CopyOnWriteArraySet erases all the elements in the Set. The size of the Set becomes zero after the function is called.

Syntax:

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.

public void clear()

Return Value: The function does not returns anything.

Below programs illustrate the above function:



Program 1:




// Java Program to illustrate the CopyOnWriteArraySet
// clear() method in Java
  
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArraySet
        CopyOnWriteArraySet<Integer> ArrSet
            = new CopyOnWriteArraySet<Integer>();
  
        // Add elements
        ArrSet.add(32);
        ArrSet.add(67);
        ArrSet.add(98);
        ArrSet.add(98);
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        // Clear the CopyOnWriteArraySet
        // Using clear() method
        ArrSet.clear();
  
        System.out.println("\nCopyOnWriteArraySet cleared\n");
  
        // Print the updated CopyOnWriteArraySet
        System.out.println("Updated CopyOnWriteArraySet: "
                           + ArrSet);
    }
}
Output:
CopyOnWriteArraySet: [32, 67, 98]

CopyOnWriteArraySet cleared

Updated CopyOnWriteArraySet: []

Program 2:




// Java Program to illustrate the CopyOnWriteArraySet
// clear() method in Java
  
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArraySet
        CopyOnWriteArraySet<String> ArrSet
            = new CopyOnWriteArraySet<String>();
  
        // Add elements
        ArrSet.add("gopal");
        ArrSet.add("gfg");
        ArrSet.add("jgec");
        ArrSet.add("jgec");
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        // Clear the CopyOnWriteArraySet
        // Using clear() method
        ArrSet.clear();
  
        System.out.println("\nCopyOnWriteArraySet cleared\n");
  
        // Print the updated CopyOnWriteArraySet
        System.out.println("Updated CopyOnWriteArraySet: "
                           + ArrSet);
    }
}
Output:
CopyOnWriteArraySet: [gopal, gfg, jgec]

CopyOnWriteArraySet cleared

Updated CopyOnWriteArraySet: []

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArraySet.html#clear–




My Personal Notes arrow_drop_up
Recommended Articles
Page :