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:
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 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); } } |
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 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); } } |
Linked Transfer Queue : [GeeksforGeeks, Geek, Gfg, Computer, Science] ArrayList to be retained : [Gfg, Science, Computer] Linked Transfer Queue after retaining ArrayList : [Gfg, Computer, Science]
Recommended Posts:
- List retainAll() Method in Java with Examples
- CopyOnWriteArrayList retainAll() method in Java with Examples
- Vector retainAll() method in Java with Examples
- AbstractCollection retainAll() method in Java with Examples
- LinkedBlockingDeque retainAll() method in Java with Examples
- LinkedTransferQueue toArray() method in Java with Examples
- LinkedTransferQueue getWaitingConsumerCount() method in Java with Examples
- LinkedTransferQueue removeIf() method in Java with Examples
- LinkedTransferQueue forEach() method in Java with Examples
- LinkedTransferQueue removeAll() method in Java with Examples
- LinkedTransferQueue remainingCapacity() method in Java with Examples
- LinkedTransferQueue toString() method in Java with Examples
- LinkedTransferQueue tryTransfer() method in Java with Examples
- LinkedTransferQueue transfer() method in Java with Examples
- Set 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.



