CopyOnWriteArraySet forEach() method in Java with Examples

The forEach() method of CopyOnWriteArraySet is an in-built function in Java which is used to traverse each element in this Set.

Syntax:

public void forEach (Consumer<E> action)

Parameters: This method takes a parameter action which represents the action to be performed for each element.

Return Value: This method does not returns anything.

Exceptions: This method throws NullPointerException if the specified action is null.

Below program illustrates the forEach() function of CopyOnWriteArraySet class:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate the CopyOnWriteArraySet
// forEach() 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(2);
        ArrSet.add(3);
        ArrSet.add(4);
        ArrSet.add(7);
        ArrSet.add(8);
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
        System.out.println("Traversing this Set: ");
  
        // Traverse this set using forEach() method
        ArrSet.forEach((n) -> System.out.println(n));
    }
}

chevron_right


Output:

CopyOnWriteArraySet: [2, 3, 4, 7, 8]
Traversing this Set: 
2
3
4
7
8

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate the CopyOnWriteArraySet
// forEach() 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("GeeksforGeeks");
        ArrSet.add("Geeks");
        ArrSet.add("Computer Science");
        ArrSet.add("Portal");
        ArrSet.add("Gfg");
  
        // Print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
        System.out.println("Traversing this Set: ");
  
        // Traverse this set using forEach() method
        ArrSet.forEach((n) -> System.out.println(n));
    }
}

chevron_right


Output:

CopyOnWriteArraySet: [GeeksforGeeks, Geeks, Computer Science, Portal, Gfg]
Traversing this Set: 
GeeksforGeeks
Geeks
Computer Science
Portal
Gfg

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/CopyOnWriteArraySet.html#forEach-java.util.function.Consumer-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.