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:
Java
Java
public boolean removeAll(Collection c)Parameters: This method takes collection c as a parameter containing elements to be removed from this list. Returns: This method returns true if this list changed as a result of the call. Exceptions: NULL Pointer Exception if this list contains a null element. Below program illustrates the removeAll() function of LinkedTransferQueue class : Program 1:
// Java code to illustrate
// removeAll() 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("Geeks");
LTQ.add("Computer Science");
LTQ.add("Portal");
LTQ.add("Gfg");
// Print the Queue
System.out.println("Linked Transfer Queue : "
+ LTQ);
// Get the ArrayList to be deleted
ArrayList<String> arraylist
= new ArrayList<String>();
arraylist.add("GeeksforGeeks");
arraylist.add("Gfg");
arraylist.add("hack");
// Print ArrayList
System.out.println("ArrayList to be deleted : "
+ arraylist);
// Removing elements from the queue
// which are common to arraylist
// using removeAll() method.
LTQ.removeAll(arraylist);
// Prints the Queue
System.out.println("Linked Transfer Queue "
+ "after removal of ArrayList : "
+ LTQ);
}
}
Output:
Program 2:
Linked Transfer Queue : [GeeksforGeeks, Geeks, Computer Science, Portal, Gfg] ArrayList to be deleted : [GeeksforGeeks, Gfg, hack] Linked Transfer Queue after removal of ArrayList : [Geeks, Computer Science, Portal]
// Java code to illustrate
// removeAll() 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 deleted
ArrayList<Integer> arraylist
= new ArrayList<Integer>();
arraylist.add(10);
arraylist.add(100);
arraylist.add(125);
// Print ArrayList
System.out.println("ArrayList to be deleted : "
+ arraylist);
// Removing elements from the queue
// which are common to arraylist
// using removeAll() method.
LTQ.removeAll(arraylist);
// Prints the Queue
System.out.println("Linked Transfer Queue "
+ "after removal of ArrayList : "
+ LTQ);
}
}
Output:
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedTransferQueue.html#removeAll-java.util.Collection-Linked Transfer Queue : [3, 6, 10, 125, 205] ArrayList to be deleted : [10, 100, 125] Linked Transfer Queue after removal of ArrayList : [3, 6, 205]