HomeLanguagesJavaCopyOnWriteArrayList forEach() method in Java with Examples

CopyOnWriteArrayList forEach() method in Java with Examples

The forEach() method of CopyOnWriteArrayList performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

Syntax:

public void forEach (Consumer<E> action)

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

Returns: This method does not returns anything.

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

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

Program 1:




// Java Program to illustrate the CopyOnWriteArrayList
// forEach() method in Java
  
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<Integer> ArrLis
            = new CopyOnWriteArrayList<Integer>();
  
        // Add elements
        ArrLis.add(2);
        ArrLis.add(3);
        ArrLis.add(4);
        ArrLis.add(7);
  
        // Print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList: "
                           + ArrLis);
  
        System.out.println("Traversing this List : ");
  
        // Traverse this queue using forEach() method
        ArrLis.forEach((n) -> System.out.println(n));
    }
}


Output:

CopyOnWriteArrayList: [2, 3, 4, 7]
Traversing this List : 
2
3
4
7

Program 2:




// Java Program to illustrate the CopyOnWriteArrayList
// forEach() method in Java
  
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Create object of CopyOnWriteArrayList
        CopyOnWriteArrayList<String> ArrLis
            = new CopyOnWriteArrayList<String>();
  
        // Add elements
        ArrLis.add("Geeks");
        ArrLis.add("Gfg");
        ArrLis.add("Portal");
        ArrLis.add("neveropen");
  
        // Print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList: "
                           + ArrLis);
  
        System.out.println("Traversing this List : ");
  
        // Traverse this queue using forEach() method
        ArrLis.forEach((n) -> System.out.println(n));
    }
}


Output:

CopyOnWriteArrayList: [Geeks, Gfg, Portal, neveropen]
Traversing this List : 
Geeks
Gfg
Portal
neveropen

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

RELATED ARTICLES

5 COMMENTS

Most Popular

Dominic
32738 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7124 POSTS0 COMMENTS
Nicole Veronica
12254 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12337 POSTS0 COMMENTS
Shaida Kate Naidoo
7235 POSTS0 COMMENTS
Ted Musemwa
7497 POSTS0 COMMENTS
Thapelo Manthata
7214 POSTS0 COMMENTS
Umr Jansen
7184 POSTS0 COMMENTS