Home » Java ConcurrentLinkedQueue forEach() Method

Java ConcurrentLinkedQueue forEach() Method

by Online Tutorials Library

Java ConcurrentLinkedQueue forEach() Method

The forEach() method of ConcurrentLinkedQueue class executes the specified action for each element of Iterable, until all the elements are processed, or the action throws an exception.

Syntax:

Parameters:

action – this is the action to be performed for every element.

Specified By:

The forEach() method of ConcurrentLinkedQueue class is specified by :

forEach() method in interface Iterable<E>.

Throws:

The forEach() method throws NullPointerException if the defined action represents null.

Example 1

Test it Now

Output:

[Reema, Rahul, Rita, Ramesh]    Printing the queue with the help of foreach loop :   Reema  Rahul  Rita  Ramesh  

Example 2

Test it Now

Output:

1.Roll No. =  15cs1029    College =  MVN University    Name = Reema    2.Roll No. =  15cs1010    College =  MVN University    Name = Geetanjali    3.Roll No. =  17cs1029    College =  MR University    Name = Vineet    4.Roll No. =  15cs1011    College =  MVN University    Name = Himanshu  

Example 3

Test it Now

Output:

1 Lower case = reema    Upper case = REEMA    2 Lower case = sonia    Upper case = SONIA  

Example 4

Test it Now

Output:

Exception in thread "main" java.lang.NullPointerException  at java.util.concurrent.ConcurrentLinkedQueue.checkNotNull(ConcurrentLinkedQueue.java:920)  at java.util.concurrent.ConcurrentLinkedQueue.offer(ConcurrentLinkedQueue.java:327)  at java.util.concurrent.ConcurrentLinkedQueue.add(ConcurrentLinkedQueue.java:297)  at com.tutorAspire.ConcurrentLinkedQueueForEachExample4.main(ConcurrentLinkedQueueForEachExample4.java:7)  

You may also like