LinkedBlockingDeque forEach() method in Java with Examples
The forEach() method of LinkedBlockingDeque 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.
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 LinkedBlockingDeque class:
Example:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>();
LBD.add( 11 );
LBD.add( 22 );
LBD.add( 33 );
LBD.add( 44 );
LBD.add( 55 );
LBD.add( 66 );
LBD.add( 77 );
System.out.println( "Linked Blocking Deque: "
+ LBD);
System.out.println( "Traversing this Deque: " );
LBD.forEach((n) -> System.out.println(n));
}
}
|
Output:
Linked Blocking Deque: [11, 22, 33, 44, 55, 66, 77]
Traversing this Deque:
11
22
33
44
55
66
77
Example: 2
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
LinkedBlockingDeque<String> LBD
= new LinkedBlockingDeque<String>();
LBD.add( "GeeksforGeeks" );
LBD.add( "Gfg" );
LBD.add( "Geeks" );
LBD.add( "Computer" );
LBD.add( "Science" );
LBD.add( "Portal" );
System.out.println( "Linked Blocking Deque: "
+ LBD);
System.out.println( "Traversing this deque: " );
LBD.forEach((n) -> System.out.println(n));
}
}
|
Output:
Linked Blocking Deque: [GeeksforGeeks, Gfg, Geeks, Computer, Science, Portal]
Traversing this deque:
GeeksforGeeks
Gfg
Geeks
Computer
Science
Portal
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedBlockingDeque.html#forEach-java.util.function.Consumer-
Last Updated :
19 Mar, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...