Open In App

CopyOnWriteArraySet iterator() method in Java

Last Updated : 26 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The iterator() method of CopyOnWriteArraySet returns an iterator over the elements in this Set in proper sequence. The iterator does NOT support the remove method.

Syntax:

public Iterator iterator()

Return Value: The function returns an iterator over the elements in the Set.

Below programs illustrate the above function:

Program 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());
        }
    }
}


Output:

CopyOnWriteArraySet: [32, 67, 100]

The iterator values of CopyOnWriteArraySet are:

32
67
100

Program 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());
        }
    }
}


Output:

CopyOnWriteArraySet: [gopal, gfg, jgec, sudo]

The iterator values of CopyOnWriteArraySet are:

gopal
gfg
jgec
sudo

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



Similar Reads

CopyOnWriteArraySet add() method in Java
The add(E e) method of CopyOnWriteArraySet inserts the element passed in the parameter to the end of the Set or at a specified index in the Set. The function returns true on addition of new element to the Set. Syntax: public boolean add(E e) Parameters: The function accepts a single mandatory parameter element which specifies the element to be adde
2 min read
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: // Java Program to illustrate the CopyOnWriteArraySet // clear() metho
2 min read
CopyOnWriteArraySet contains() method in Java
The contains(E e) method of CopyOnWriteArraySet checks if a given element is present in the Set or not. Syntax: public boolean contains(Object o) Parameters: The function accepts a single mandatory parametero which specifies the element whose appearance is to be checked in the Set. Return Value: The function returns true if the element is present e
2 min read
CopyOnWriteArraySet equals() method in Java
The equals(Object O) method of CopyOnWriteArraySet compares the specified object with this set for equality. Returns true if the specified object is the same object as this object, or if it is also a Set and the elements returned by an iterator over the specified set are the same as the elements returned by an iterator over this set. More formally,
2 min read
CopyOnWriteArraySet isEmpty() method in Java
The isEmpty(E e) method of CopyOnWriteArraySet checks if the Set is empty or not. It returns true if the Set is empty, else it returns false. Syntax: public boolean isEmpty() Return Value: The function returns true if the Set is empty, otherwise it returns false. Below programs illustrate the above function: Program 1: Java Code // Java Program to
1 min read
CopyOnWriteArraySet size() method in Java
The size() method of CopyOnWriteArraySet returns the size of the Set. It returns the number of elements in the current Set. Syntax: public int size() Return Value: The function returns the number of elements in the current Set. Below programs illustrate the above function: Program 1: // Java Program to illustrate the CopyOnWriteArraySet // size() m
2 min read
CopyOnWriteArraySet remove() method in Java
The remove() method of CopyOnWriteArraySet removes the specified element if it is present in the set. Syntax: public boolean remove(Object o) Parameters: The function accepts a mandatory parameter o which specifies the element to be removed from the set if present. Return Value: The function returns true if set contains the specified element. Below
2 min read
CopyOnWriteArraySet spliterator() method in Java
The spliterator() method of CopyOnWriteArraySet returns an spliterator over the elements in this Set in proper sequence. There is no need of synchronization while operating on the spliterator. Syntax: public Spliterator spliterator() Return Value: The function returns an spliterator over the elements in the Set. Below programs illustrate the above
2 min read
CopyOnWriteArraySet toArray() method in Java with Example
toArray() The Java.util.concurrent.CopyOnWriteArraySet.toArray() method returns an array containing all the elements in the set in proper sequence i.e. from first to last. The returned array will be safe as a new array is created (hence new memory is allocated). Thus the caller is free to modify the array. It acts as a bridge between array-based an
3 min read
CopyOnWriteArraySet containsAll() method in Java with Example
The containsAll() method of Java CopyOnWriteArraySet is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set. Syntax: public boolean containsAll(Collection C) Parameters: The parameter C is a Collection. This parameter refers
2 min read