El método iterator() de CopyOnWriteArraySet devuelve un iterador sobre los elementos de este conjunto en la secuencia adecuada. El iterador NO admite el método de eliminación.
Sintaxis:
public Iterator iterator()
Valor devuelto: la función devuelve un iterador sobre los elementos del Conjunto.
Los siguientes programas ilustran la función anterior:
Programa 1:
// Java Program to illustrate the CopyOnWriteArraySet // iterator() 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(67); ArrSet.add(100); // print CopyOnWriteArraySet System.out.println("CopyOnWriteArraySet: " + ArrSet); // Call iterator() method of Iterator iteratorVals = ArrSet.iterator(); // Print elements of iterator // created from CopyOnWriteArraySet System.out.println("\nThe iterator values of " + "CopyOnWriteArraySet are:\n"); // prints the elements using an iterator while (iteratorVals.hasNext()) { System.out.println(iteratorVals.next()); } } }
Producción:
CopyOnWriteArraySet: [32, 67, 100] The iterator values of CopyOnWriteArraySet are: 32 67 100
Programa 2:
// Java Program to illustrate the CopyOnWriteArraySet // iterator() 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); // Call iterator() method of Iterator iteratorVals = ArrSet.iterator(); // Print elements of iterator // created from CopyOnWriteArraySet System.out.println("\nThe iterator values of " + "CopyOnWriteArraySet are:\n"); // prints the elements using an iterator while (iteratorVals.hasNext()) { System.out.println(iteratorVals.next()); } } }
Producción:
CopyOnWriteArraySet: [gopal, gfg, jgec, sudo] The iterator values of CopyOnWriteArraySet are: gopal gfg jgec sudo
Referencia: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArraySet.html#iterator–