Java中的 LinkedBlockingDeque removeAll() 方法及示例
LinkedBlockingDeque 的removeAll()方法是Java的一个内置函数,用于从该双端队列中删除指定集合中包含的所有元素。这意味着,这两个集合的所有公共元素都从这个双端队列中删除。
句法:
public boolean removeAll(Collection c)
参数:此方法将集合c作为参数,其中包含要从此列表中删除的元素。
返回值:如果此双端队列因调用而更改,则此方法返回 true。
异常:如果指定的集合为 Null,此方法将引发NULL 指针异常。
下面的程序说明了 LinkedBlockingDeque 类的 removeAll()函数:
示例 1:
Java
// Java Program Demonstrate removeAll()
// method of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque LBD
= new LinkedBlockingDeque();
// Add numbers to end of LinkedBlockingDeque
LBD.add(11);
LBD.add(22);
LBD.add(33);
LBD.add(44);
// print deque
System.out.println("Linked Blocking Deque: "
+ LBD);
// create object of ArrayList collection
ArrayList ArrLis
= new ArrayList();
// Add number to ArrayList
ArrLis.add(55);
ArrLis.add(11);
ArrLis.add(23);
ArrLis.add(22);
// print ArrayList
System.out.println("ArrayList to be removed: "
+ ArrLis);
// function removeAll() removes all
// the common elements from deque
LBD.removeAll(ArrLis);
// print deque
System.out.println("Current Linked Blocking Deque: "
+ LBD);
}
}
Java
// Java Program Demonstrate removeAll()
// method of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque LBD
= new LinkedBlockingDeque();
// Add numbers to end of LinkedBlockingDeque
LBD.add("geeks");
LBD.add("Geeks");
LBD.add("gfg");
LBD.add("Gfg");
// print deque
System.out.println("Linked Blocking Deque: "
+ LBD);
// create object of ArrayList collection
ArrayList ArrLis
= new ArrayList();
// Add number to ArrayList
ArrLis.add("GeeksforGeeks");
ArrLis.add("Geeks");
ArrLis.add("gfg");
ArrLis.add("Gfg");
// print ArrayList
System.out.println("ArrayList: "
+ ArrLis);
// function removeAll() removes all
// the common elements from deque
LBD.removeAll(ArrLis);
// print deque
System.out.println("Linked Blocking Deque: "
+ LBD);
}
}
输出
Linked Blocking Deque: [11, 22, 33, 44]
ArrayList to be removed: [55, 11, 23, 22]
Current Linked Blocking Deque: [33, 44]
示例 2:
Java
// Java Program Demonstrate removeAll()
// method of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque LBD
= new LinkedBlockingDeque();
// Add numbers to end of LinkedBlockingDeque
LBD.add("geeks");
LBD.add("Geeks");
LBD.add("gfg");
LBD.add("Gfg");
// print deque
System.out.println("Linked Blocking Deque: "
+ LBD);
// create object of ArrayList collection
ArrayList ArrLis
= new ArrayList();
// Add number to ArrayList
ArrLis.add("GeeksforGeeks");
ArrLis.add("Geeks");
ArrLis.add("gfg");
ArrLis.add("Gfg");
// print ArrayList
System.out.println("ArrayList: "
+ ArrLis);
// function removeAll() removes all
// the common elements from deque
LBD.removeAll(ArrLis);
// print deque
System.out.println("Linked Blocking Deque: "
+ LBD);
}
}
输出
Linked Blocking Deque: [geeks, Geeks, gfg, Gfg]
ArrayList: [GeeksforGeeks, Geeks, gfg, Gfg]
Linked Blocking Deque: [geeks]
参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/concurrent/LinkedBlockingDeque.html#removeAll-java.util.Collection-