VOOZH about

URL: https://www.geeksforgeeks.org/java/copyonwritearraylist-foreach-method-in-java-with-examples/

⇱ CopyOnWriteArrayList forEach() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CopyOnWriteArrayList forEach() method in Java with Examples

Last Updated : 11 Jul, 2025
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:
Output:
CopyOnWriteArrayList: [2, 3, 4, 7]
Traversing this List : 
2
3
4
7
Program 2:
Output:
CopyOnWriteArrayList: [Geeks, Gfg, Portal, geeksforgeeks]
Traversing this List : 
Geeks
Gfg
Portal
geeksforgeeks
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#forEach-java.util.function.Consumer-
Comment