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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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:
CopyOnWriteArraySet: [2, 3, 4, 7, 8] Traversing this Set: 2 3 4 7 8
CopyOnWriteArraySet: [GeeksforGeeks, Geeks, Computer Science, Portal, Gfg] Traversing this Set: GeeksforGeeks Geeks Computer Science Portal Gfg