LinkedTransferQueue retainAll() method in Java with Examples Last Updated : 08 Mar, 2019 Comments Improve Suggest changes Like Article Like Report 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: public boolean retainAll(Collection C) Parameters: This method takes the parameter C which is the collection containing elements to be retained in the queue. Returns: The method returns a boolean value true if the queue is changed at all as a result of the call else false. Exceptions: This method throws following exceptions: ClassCastException: If the class of an element of this Queue is incompatible with the Passed collection. NullPointerException: If the specified collection is Null. Below program illustrates the retainAll() function of LinkedTransferQueue class : Program 1: Java // Java code to illustrate // retainAll() method of LinkedTransferQueue import java.util.concurrent.LinkedTransferQueue; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of LinkedTransferQueue LinkedTransferQueue<Integer> LTQ = new LinkedTransferQueue<Integer>(); // Add numbers to end of LinkedTransferQueue // using add() method LTQ.add(3); LTQ.add(6); LTQ.add(10); LTQ.add(125); LTQ.add(205); // Print the Queue System.out.println("Linked Transfer Queue : " + LTQ); // Get the ArrayList to be retained ArrayList<Integer> arraylist = new ArrayList<Integer>(); arraylist.add(10); arraylist.add(100); arraylist.add(125); // Print ArrayList System.out.println("ArrayList to be retained : " + arraylist); // Retaining elements from the queue // which are common to arraylist // using retainAll() method. LTQ.retainAll(arraylist); // Prints the Queue System.out.println("Linked Transfer Queue " + "after retaining ArrayList : " + LTQ); } } Output: Linked Transfer Queue : [3, 6, 10, 125, 205] ArrayList to be retained : [10, 100, 125] Linked Transfer Queue after retaining ArrayList : [10, 125] Program 2: Java // Java code to illustrate // retainAll() method of LinkedTransferQueue import java.util.concurrent.LinkedTransferQueue; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of LinkedTransferQueue LinkedTransferQueue<String> LTQ = new LinkedTransferQueue<String>(); // Add numbers to end of LinkedTransferQueue // using add() method LTQ.add("GeeksforGeeks"); LTQ.add("Geek"); LTQ.add("Gfg"); LTQ.add("Computer"); LTQ.add("Science"); // Print the Queue System.out.println("Linked Transfer Queue : " + LTQ); // Get the ArrayList to be retained ArrayList<String> arraylist = new ArrayList<String>(); arraylist.add("Gfg"); arraylist.add("Science"); arraylist.add("Computer"); // Print ArrayList System.out.println("ArrayList to be retained : " + arraylist); // Retaining elements from the queue // which are common to arraylist // using retainAll() method. LTQ.retainAll(arraylist); // Prints the Queue System.out.println("Linked Transfer Queue " + "after retaining ArrayList : " + LTQ); } } Output: Linked Transfer Queue : [GeeksforGeeks, Geek, Gfg, Computer, Science] ArrayList to be retained : [Gfg, Science, Computer] Linked Transfer Queue after retaining ArrayList : [Gfg, Computer, Science] Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedTransferQueue.html#retainAll-java.util.Collection- Comment More infoAdvertise with us Next Article LinkedTransferQueue retainAll() method in Java with Examples P ProgrammerAnvesh Follow Improve Article Tags : Java Java - util package Java-LinkedTransferQueue Practice Tags : Java Similar Reads LinkedTransferQueue removeAll() method in Java with Examples The removeAll() method of java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to remove from this queue all of its elements that are contained in the specified collection. Syntax: public boolean removeAll(Collection c) Parameters: This method takes collection c as 3 min read LinkedTransferQueue toArray() method in Java with Examples public Object[] toArray() The toArray() method of Java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to form an array of the same elements as that of LinkedTransferQueue. Basically, it copies all the element to the new array from the LinkedTransferQueue. Syntax: p 3 min read LinkedTransferQueue removeIf() method in Java with Examples The removeIf() method of java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to remove all of the elements of this queue that satisfies a given predicate filter which is passed as a parameter to the method. Syntax: public boolean removeIf(Predicate filter) Paramete 2 min read LinkedTransferQueue transfer() method in Java with Examples The transfer() method of class LinkedTransferQueue is an inbuilt function in Java which is generally used to transfer an element to a thread which is waiting to receive it, if there is no thread waiting then it will wait till a thread comes to waiting state as soon as waiting thread arrives element 2 min read LinkedTransferQueue tryTransfer() method in Java with Examples The tryTransfer() method of class LinkedTransferQueue is an inbuilt function in Java which is generally used to transfer an element to a thread which is waiting to receive it, if in case there is no thread waiting then it will terminate without adding element into the queue or you can also make it w 5 min read LinkedTransferQueue toString() method in Java with Examples The toString() method of java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to return the string representation of the collection. The String representation consists of the elements of the collection enclosed in "[]" and separated by ", ". The order of elements in 2 min read LinkedTransferQueue remainingCapacity() method in Java with Examples The Java.util.concurrent.LinkedTransferQueue.remainingCapacity(): method always returns Integer.MAX_VALUE as this kind of queue is not capacity constrained. Syntax public int remainingCapacity() Parameters: This method does not take in any parameters. Returns: This method returns only one value i.e. 1 min read 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 reta 2 min read LinkedHashSet retainAll() method in Java with Example The retainAll() method of java.util.LinkedHashSet class is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from 3 min read LinkedTransferQueue forEach() method in Java with Examples 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 fo 2 min read Like