Java ArrayBlockingQueue 类的 retainAll() 方法仅保留指定集合中包含的 ArrayBlockingQueue 元素并删除其余元素。
用法:
public boolean retainAll(Collection<?> c)
参数:
这里,参数 c 是包含要保留在 ArrayBlockingQueue 集合中的元素的集合。
指定者:
ArrayBlockingQueue 类的retainAll() 方法指定为:
retainAll in interface Collection<E>.
返回值:
如果集合因调用而改变,则retainAll() 方法返回一个布尔值'true',否则返回'false'。
抛出:
如果指定的集合不支持空元素或包含一个或多个空元素,则抛出 NullPointerException。
例子1
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
public class ArrayBlockingQueueRetainAllExample1 {
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<21;i++){
queue.add(i);
}
System.out.println(" Elements in queue:" + queue);
for (int i = 1; i < 11; i++) {
int j = i *5;
list.add(j);
}
//will give the elements in the queue which are present in list
queue.retainAll(list);
System.out.println(" Multiple of 5:" + queue);
}
}
输出:
Elements in queue:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Multiple of 5:[5, 10, 15, 20]
例子2
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
public class ArrayBlockingQueueRetainAllExample2 {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
int capacity = 100;
ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(capacity);
queue.add("Reema");
queue.add("Ranjeeta");
queue.add("Rani");
queue.add("Sukla");
queue.add("Raj");
System.out.println("Students in the class:");
for (String str:queue){
System.out.println(str);
}
list.add("Raj");
list.add("Rani");
list.add("Ranjeeta");
queue.retainAll(list);
System.out.println("Students failed:");
for (String str:queue){
System.out.println(str);
}
}
}
输出:
Students in the class: Reema Ranjeeta Rani Sukla Raj Students failed: Ranjeeta Rani Raj
例子3
import java.util.TreeSet;
import java.util.concurrent.ArrayBlockingQueue;
public class ArrayBlockingQueueRetainAllExample3 {
public static void main(String[] args) {
TreeSet<Integer> list = new TreeSet<Integer>();
int capacity = 100;
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
for (int i=1;i<21;i++){
queue.add(i);
}
list.add(101);
list.add(null);
queue.retainAll(list);
System.out.println("Queue = "+queue);
}
}
输出:
Exception in thread "main" java.lang.NullPointerException at java.util.TreeMap.put(TreeMap.java:563) at java.util.TreeSet.add(TreeSet.java:255) at com.javaTpoint.ArrayBlockingQueueRetainAllExample3.main(ArrayBlockingQueueRetainAllExample3.java:15)
由于 TreeSet 不允许空元素,所以它会给出 NullPointerException,如上所示。
相关用法
- Java ArrayBlockingQueue removeIf()用法及代码示例
- Java ArrayBlockingQueue remainingCapacity()用法及代码示例
- Java ArrayBlockingQueue remove()用法及代码示例
- Java ArrayBlockingQueue removeAll()用法及代码示例
- 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 retainAll() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。