El método clear() de CopyOnWriteArraySet borra todos los elementos del Conjunto. El tamaño del conjunto se convierte en cero después de llamar a la función.
Sintaxis:
public void clear()
Valor devuelto: la función no devuelve nada.
Los siguientes programas ilustran la función anterior:
Programa 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); } }
Producción:
CopyOnWriteArraySet: [32, 67, 98] CopyOnWriteArraySet cleared Updated CopyOnWriteArraySet: []
Programa 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); } }
Producción:
CopyOnWriteArraySet: [gopal, gfg, jgec] CopyOnWriteArraySet cleared Updated CopyOnWriteArraySet: []
Referencia: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArraySet.html#clear–