CopyOnWriteArraySet clear() method in Java

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:

public void clear()

Return Value: The function does not returns anything.

Below programs illustrate the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

CopyOnWriteArraySet: [32, 67, 98]

CopyOnWriteArraySet cleared

Updated CopyOnWriteArraySet: []

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.