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]
Please Login to comment...