El método size() de CopyOnWriteArraySet devuelve el tamaño del conjunto. Devuelve el número de elementos del Conjunto actual.
Sintaxis:
public int size()
Valor devuelto: la función devuelve el número de elementos en el conjunto actual.
Los siguientes programas ilustran la función anterior:
Programa 1:
// Java Program to illustrate the CopyOnWriteArraySet // size() 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>(); // print CopyOnWriteArraySet System.out.println("CopyOnWriteArraySet: " + ArrSet); // size value print System.out.println("size of ArrSet: " + ArrSet.size()); // Add elements ArrSet.add(32); ArrSet.add(67); ArrSet.add(98); ArrSet.add(100); // print CopyOnWriteArraySet System.out.println("CopyOnWriteArraySet: " + ArrSet); // size value print System.out.println("size of ArrSet: " + ArrSet.size()); } }
Producción:
CopyOnWriteArraySet: [] size of ArrSet: 0 CopyOnWriteArraySet: [32, 67, 98, 100] size of ArrSet: 4
Programa 2:
// Java Program to illustrate the CopyOnWriteArraySet // size() 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("sudo"); // print CopyOnWriteArraySet System.out.println("CopyOnWriteArraySet: " + ArrSet); // size value print System.out.println("size of ArrSet: " + ArrSet.size()); } }
Producción:
CopyOnWriteArraySet: [gopal, gfg, jgec, sudo] size of ArrSet: 4
Referencia: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArraySet.html#size–