LinkedBlockingDeque retainAll() method in Java with Examples
Last Updated :
19 Mar, 2019
The retainAll() method of LinkedBlockingDeque is an in-built function is Java which is used to remove from this deque all of its elements that are contained in the specified collection. That means, all the common elements these two collections are removed from this deque.
Syntax:
public boolean retainAll(Collection c)
Parameters: This method takes collection c as a parameter containing elements to be removed from this list.
Return Value: This method returns true if this deque changed as a result of the call.
Exceptions: This method throws NULL Pointer Exception if the specified collection is Null.
Below program illustrates the retainAll() function of LinkedBlockingDeque class:
Example 1:
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 );
System.out.println( "Linked Blocking Deque: "
+ LBD);
ArrayList<Integer> ArrLis
= new ArrayList<Integer>();
ArrLis.add( 55 );
ArrLis.add( 11 );
ArrLis.add( 23 );
ArrLis.add( 22 );
System.out.println( "ArrayList to be retained: "
+ ArrLis);
LBD.retainAll(ArrLis);
System.out.println( "Retained Linked Blocking Deque: "
+ LBD);
}
}
|
Output:
Linked Blocking Deque: [11, 22, 33, 44]
ArrayList to be retained: [55, 11, 23, 22]
Retained Linked Blocking Deque: [11, 22]
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( "geeks" );
LBD.add( "Geeks" );
LBD.add( "gfg" );
LBD.add( "Gfg" );
System.out.println( "Linked Blocking Deque: "
+ LBD);
ArrayList<String> ArrLis
= new ArrayList<String>();
ArrLis.add( "GeeksforGeeks" );
ArrLis.add( "Geeks" );
ArrLis.add( "gfg" );
ArrLis.add( "Gfg" );
System.out.println( "ArrayList to be retained: "
+ ArrLis);
LBD.retainAll(ArrLis);
System.out.println( "Retained Linked Blocking Deque: "
+ LBD);
}
}
|
Output:
Linked Blocking Deque: [geeks, Geeks, gfg, Gfg]
ArrayList to be retained: [GeeksforGeeks, Geeks, gfg, Gfg]
Retained Linked Blocking Deque: [Geeks, gfg, Gfg]
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedBlockingDeque.html#retainAll-java.util.Collection-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...