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.
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 :
Linked Transfer Queue : [3, 6, 10, 125, 205] ArrayList to be retained : [10, 100, 125] Linked Transfer Queue after retaining ArrayList : [10, 125]
Linked Transfer Queue : [GeeksforGeeks, Geek, Gfg, Computer, Science] ArrayList to be retained : [Gfg, Science, Computer] Linked Transfer Queue after retaining ArrayList : [Gfg, Computer, Science]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.