Java ArrayBlockingQueue 类的 removeAll() 方法删除指定集合中包含的 ArrayBlockingQueue 元素。
用法:
public boolean removeAll(Collection<?> c)
参数:
这里,参数 c 是包含要从 ArrayBlockingQueue 中删除的元素的集合。
指定者:
ArrayBlockingQueue 类的 removeAll() 方法指定为:
接口 Collection<E> 中的 removeAll。
返回值:
如果集合因调用而改变,removeAll() 方法返回一个布尔值 'true',否则返回 'false'。
抛出:
如果指定的集合不支持空元素或包含一个或多个空元素,则抛出 NullPointerException。
例子1
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
public class ArrayBlockingQueueRemoveAllExample1 {
public static void main(String[] args) throws InterruptedException {
List<Integer> list = new ArrayList<Integer>();
int capacity = 100;
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
for (int i=1;i<11;i++){
queue.add(i);}
System.out.println("Total no:"+ queue);
for (int i=1;i<11;i++){
i=i+1;
list.add(i);}
//removes all the elements int the queue which are present in list
queue.removeAll(list);
System.out.println(" Even no:"+list);
System.out.println(" Odd no:"+queue);
}
}
输出:
Total no:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Even no:[2, 4, 6, 8, 10] Odd no:[1, 3, 5, 7, 9]
例子2
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
class Patients{
String Pname;
public Patients(String pname) {
Pname = pname;
}
public String toString(){
return this.Pname;
}
}
public class ArrayBlockingQueueRemoveAllExample2 {
public static void main(String[] args) {
Patients patient1 = new Patients("Megha");
Patients patient2 = new Patients("Reema");
Patients patient3 = new Patients("Supriya");
Patients patient4 = new Patients("Anoop");
Patients patient5 = new Patients("Sonia");
Patients patient6 = new Patients("Tejashvi");
List<Patients> list = new ArrayList<Patients>();
int capacity = 100;
ArrayBlockingQueue<Patients> queue = new ArrayBlockingQueue<Patients>(capacity);
queue.add(patient1);
queue.add(patient2);
queue.add(patient3);
queue.add(patient4);
queue.add(patient5);
queue.add(patient6);
list.add(patient1);
list.add(patient2);
list.add(patient3);
list.add(patient4);
System.out.println("Total Patients = "+queue.size());
for(Patients xyz:queue){
System.out.println(xyz);
}
System.out.println();
System.out.println("Patients recovered and discharged:"+list);
System.out.println();
//removes all the elements int the queue which are present in list
queue.removeAll(list);
System.out.println("Patients left in the hospital = "+queue.size());
for (Patients xyz:queue) {
System.out.println(xyz);
}
}
}
输出:
Total Patients = 6 Megha Reema Supriya Anoop Sonia Tejashvi Patients recovered and discharged:[Megha, Reema, Supriya, Anoop] Patients left in the hospital = 2 Sonia Tejashvi
例子3
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
public class ArrayBlockingQueueRemoveAllExample3 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
int capacity = 100;
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
queue.add(123);
queue.add(56);
queue.add(100);
System.out.println("Queue = "+queue);
queue.removeAll(null);
System.out.println("List = "+list);
}
}
输出:
Queue = [123, 56, 100] Exception in thread "main" java.lang.NullPointerException at java.util.Objects.requireNonNull(Objects.java:203) at java.util.AbstractCollection.removeAll(AbstractCollection.java:371) at com.javaTpoint.ArrayBlockingQueueRemoveAllExample3.main(ArrayBlockingQueueRemoveAllExample3.java:16)
相关用法
- Java ArrayBlockingQueue removeIf()用法及代码示例
- Java ArrayBlockingQueue remove()用法及代码示例
- Java ArrayBlockingQueue remainingCapacity()用法及代码示例
- Java ArrayBlockingQueue retainAll()用法及代码示例
- Java ArrayBlockingQueue peek()用法及代码示例
- Java ArrayBlockingQueue put()用法及代码示例
- Java ArrayBlockingQueue Spliterator()用法及代码示例
- Java ArrayBlockingQueue drainTo()用法及代码示例
- Java ArrayBlockingQueue toArray()用法及代码示例
- Java ArrayBlockingQueue contains()用法及代码示例
- Java ArrayBlockingQueue take()用法及代码示例
- Java ArrayBlockingQueue forEach()用法及代码示例
- Java ArrayBlockingQueue clear()用法及代码示例
- Java ArrayBlockingQueue iterator()用法及代码示例
- Java ArrayBlockingQueue spliterator()用法及代码示例
- Java ArrayBlockingQueue offer()用法及代码示例
- Java ArrayBlockingQueue toString()用法及代码示例
- Java ArrayBlockingQueue add()用法及代码示例
- Java ArrayBlockingQueue poll()用法及代码示例
- Java ArrayBlockingQueue size()用法及代码示例
注:本文由纯净天空筛选整理自 Java ArrayBlockingQueue removeAll() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。