Java中的 LinkedTransferQueue removeAll() 方法及示例
Java .util.concurrent.LinkedTransferQueue 的removeAll()方法是Java的一个内置函数,用于从该队列中删除指定集合中包含的所有元素。
句法:
public boolean removeAll(Collection c)
参数:此方法将集合 c 作为参数,其中包含要从此列表中删除的元素。
返回:如果此列表因调用而更改,则此方法返回 true。
异常:如果此列表包含空元素,则空指针异常。
下面的程序说明了 LinkedTransferQueue 类的 removeAll()函数:
方案一:
// 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 LTQ
= new LinkedTransferQueue();
// 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 arraylist
= new ArrayList();
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);
}
}
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 LTQ
= new LinkedTransferQueue();
// 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 arraylist
= new ArrayList();
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);
}
}
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]
参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/concurrent/LinkedTransferQueue.html#removeAll-java.util.Collection-