Java ConcurrentLinkedQueue forEach() MethodThe 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 1Test it NowOutput: [Reema, Rahul, Rita, Ramesh] Printing the queue with the help of foreach loop : Reema Rahul Rita Ramesh Example 2Test it NowOutput: 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 3Test it NowOutput: 1 Lower case = reema Upper case = REEMA 2 Lower case = sonia Upper case = SONIA Example 4Test it NowOutput: 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.javaTpoint.ConcurrentLinkedQueueForEachExample4.main(ConcurrentLinkedQueueForEachExample4.java:7)
Next TopicJava ConcurrentLinkedQueue
|