LinkedBlockingDeque retainAll() method in Java with Examples Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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); } } Output: Linked Blocking Deque: [11, 22, 33, 44] ArrayList to be retained: [55, 11, 23, 22] Retained Linked Blocking Deque: [11, 22] Example 2: Java // 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); } } 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- Comment More infoAdvertise with us Next Article LinkedBlockingDeque removeAll() method in Java with Examples P ProgrammerAnvesh Follow Improve Article Tags : Java Java - util package Java-Functions Java-LinkedBlockingDeque Java-Classes +1 More Practice Tags : Java Similar Reads LinkedBlockingDeque removeAll() method in Java with Examples The removeAll() 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 remov 2 min read LinkedBlockingDeque removeIf() method in Java with Examples The removeIf() method of LinkedBlockingDeque removes the element from this LinkedBlockingDeque that satisfies the specified condition. Syntax: public boolean removeIf (Predicate<? super E> filter) Parameters: This method accepts a mandatory parameter filter which is the predicate value based o 2 min read LinkedBlockingQueue take() Method in Java with Examples The take() method of LinkedBlockingQueue is used to retrieve and remove the head of this queue. If the queue is empty then it will wait until an element becomes available. This method is more efficient if working on threads and using LinkedBlockingQueue in that process. So the thread that initially 4 min read LinkedBlockingQueue put() method in Java with Examples The put(E e) method of LinkedBlockingQueue inserts element passed as parameter to method at the tail of this LinkedBlockingQueue, if queue is not full. If the queue is full, then this method will wait for space to become available and after space is available, it inserts the element to LinkedBlockin 3 min read LinkedTransferQueue retainAll() method in Java with Examples The retainAll() method of java.util.concurrent.LinkedTransferQueue is an in-built function in Java which is used to retain all the elements in this queue which are common to both i.e. the specified collection and this queue. All other elements which are not common are removed from this queue. Syntax 3 min read LinkedBlockingDeque addAll() method in Java with Examples The addAll() method of LinkedBlockingDeque appends all of the elements of the specified collection to the end of this deque.Syntax: public void addAll(Collection<E> c) Parameters: This method accepts a mandatory parameter c which is the collection to be inserted in the end of the LinkedBlockin 2 min read Like