LinkedTransferQueue forEach() method in Java with Examples
Last Updated :
08 Mar, 2019
The forEach() method of Java.util.concurrent.LinkedTransferQueue is an in-built function in Java which is used to traverse each element in this queue.
Syntax:
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 LinkedTransferQueue class:
Program 1:
import java.util.concurrent.LinkedTransferQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
LinkedTransferQueue<Integer> LTQ
= new LinkedTransferQueue<Integer>();
LTQ.add( 6 );
LTQ.add( 3 );
LTQ.add( 5 );
LTQ.add( 15 );
LTQ.add( 20 );
System.out.println( "Linked Transfer Queue : "
+ LTQ);
System.out.println( "Traversing this Queue : " );
LTQ.forEach((n) -> System.out.println(n));
}
}
|
Output:
Linked Transfer Queue : [6, 3, 5, 15, 20]
Traversing this Queue :
6
3
5
15
20
Program 2:
import java.util.concurrent.LinkedTransferQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
LinkedTransferQueue<String> LTQ
= new LinkedTransferQueue<String>();
LTQ.add( "GeeksforGeeks" );
LTQ.add( "Geeks" );
LTQ.add( "Computer Science" );
LTQ.add( "Portal" );
LTQ.add( "Gfg" );
System.out.println( "Linked Transfer Queue : "
+ LTQ);
System.out.println( "Traversing this Queue : " );
LTQ.forEach((n) -> System.out.println(n));
}
}
|
Output:
Linked Transfer Queue : [GeeksforGeeks, Geeks, Computer Science, Portal, Gfg]
Traversing this Queue :
GeeksforGeeks
Geeks
Computer Science
Portal
Gfg
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedTransferQueue.html#forEach-java.util.function.Consumer-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...