LinkedBlockingDeque retainAll() method in Java with Examples
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:
// Java Program Demonstrate retainAll() // method of LinkedBlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // Create object of LinkedBlockingDeque LinkedBlockingDeque<Integer> LBD = new LinkedBlockingDeque<Integer>(); // Add numbers to end of LinkedBlockingDeque LBD.add(11); LBD.add(22); LBD.add(33); LBD.add(44); // print deque System.out.println("Linked Blocking Deque: " + LBD); // Create object of ArrayList collection ArrayList<Integer> ArrLis = new ArrayList<Integer>(); // Add number to ArrayList ArrLis.add(55); ArrLis.add(11); ArrLis.add(23); ArrLis.add(22); // Print ArrayList System.out.println("ArrayList to be retained: " + ArrLis); // Function retainAll() retains // all the common elements from deque LBD.retainAll(ArrLis); // Print deque System.out.println("Retained Linked Blocking Deque: " + LBD); } } |
Linked Blocking Deque: [11, 22, 33, 44] ArrayList to be retained: [55, 11, 23, 22] Retained Linked Blocking Deque: [11, 22]
Example 2:
// Java Program Demonstrate retainAll() // method of LinkedBlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // Create object of LinkedBlockingDeque LinkedBlockingDeque<String> LBD = new LinkedBlockingDeque<String>(); // Add numbers to end of LinkedBlockingDeque LBD.add("geeks"); LBD.add("Geeks"); LBD.add("gfg"); LBD.add("Gfg"); // Print deque System.out.println("Linked Blocking Deque: " + LBD); // Create object of ArrayList collection ArrayList<String> ArrLis = new ArrayList<String>(); // Add elements to ArrayList ArrLis.add("GeeksforGeeks"); ArrLis.add("Geeks"); ArrLis.add("gfg"); ArrLis.add("Gfg"); // Print ArrayList System.out.println("ArrayList to be retained: " + ArrLis); // Function retainAll() retains // all the common elements from deque LBD.retainAll(ArrLis); // Print deque System.out.println("Retained Linked Blocking Deque: " + LBD); } } |
Linked Blocking Deque: [geeks, Geeks, gfg, Gfg] ArrayList to be retained: [GeeksforGeeks, Geeks, gfg, Gfg] Retained Linked Blocking Deque: [Geeks, gfg, Gfg]
Recommended Posts:
- AbstractCollection retainAll() method in Java with Examples
- LinkedTransferQueue retainAll() method in Java with Examples
- List retainAll() Method in Java with Examples
- CopyOnWriteArrayList retainAll() method in Java with Examples
- Vector retainAll() method in Java with Examples
- LinkedBlockingDeque removeIf() method in Java with Examples
- LinkedBlockingDeque addAll() method in Java with Examples
- LinkedBlockingDeque removeAll() method in Java with Examples
- LinkedBlockingDeque forEach() method in Java with Examples
- Set retainAll() method in Java with Example
- LinkedHashSet retainAll() method in Java with Example
- Stack retainAll() method in Java with Example
- HashSet retainAll() method in Java with Example
- TreeSet retainAll() method in Java with Example
- AbstractSequentialList retainAll() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



