ConcurrentLinkedQueue 类的 retainAll() 方法仅保留此队列中存在于已定义集合中的那些元素。
用法:
public boolean retainAll(Collection<?> c)指定者:
ConcurrentLinkedQueue 类的 retainAll() 方法指定为:
接口 Collection<E> 中的 retainAll() 方法。
覆盖:
ConcurrentLinkedQueue 类的 retainAll() 方法被覆盖:
AbstractCollection<E> 类中的 retainAll() 方法。
参数:
c- 该集合包含要保存在该集合中的元素。
返回值:
如果此集合因调用而更改,则 retainAll() 方法返回 true。
抛出:
如果此集合包含一个或多个空元素,则 retainAll() 方法将引发 NullPointerException。
例子1
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
public class ConcurrentLinkedQueueRetainAllExample1 {
    public static void main(String[] args) throws InterruptedException {
        List<Integer> list = new ArrayList<Integer>();
        ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>();
        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 int 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.concurrent.ConcurrentLinkedQueue;
public class ConcurrentLinkedQueueRetainAllExample2 {
    static int i=1;
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
        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(i++ +"."+str);
        }
        list.add("Raj");
        list.add("Rani");
        list.add("Ranjeeta");
        queue.retainAll(list);
        System.out.println("Students failed:");
        i=1;
        for (String str:queue){
            System.out.println(i++ +"."+str);
        }
    }
}输出:
1.Reema 2.Ranjeeta 3.Rani 4.Sukla 5.Raj Students failed: 1.Ranjeeta 2.Rani 3.Raj
例子3
import java.util.TreeSet;
import java.util.concurrent.ConcurrentLinkedQueue;
public class ConcurrentLinkedQueueRetainAllExample3 {
    public static void main(String[] args) {
        TreeSet<Integer> list = new TreeSet<Integer>();
        ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>();
        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.ConcurrentLinkedQueueRetainAllExample3.main(ConcurrentLinkedQueueRetainAllExample3.java:13)
相关用法
- Java ConcurrentLinkedQueue removeAll()用法及代码示例
 - Java ConcurrentLinkedQueue removeIf()用法及代码示例
 - Java ConcurrentLinkedQueue remove()用法及代码示例
 - Java ConcurrentLinkedQueue add()用法及代码示例
 - Java ConcurrentLinkedQueue offer()用法及代码示例
 - Java ConcurrentLinkedQueue spliterator()用法及代码示例
 - Java ConcurrentLinkedQueue size()用法及代码示例
 - Java ConcurrentLinkedQueue forEach()用法及代码示例
 - Java ConcurrentLinkedQueue iterator()用法及代码示例
 - Java ConcurrentLinkedQueue contains()用法及代码示例
 - Java ConcurrentLinkedQueue peek()用法及代码示例
 - Java ConcurrentLinkedQueue isEmpty()用法及代码示例
 - Java ConcurrentLinkedQueue addAll()用法及代码示例
 - Java ConcurrentLinkedQueue poll()用法及代码示例
 - Java ConcurrentLinkedQueue toArray()用法及代码示例
 - Java ConcurrentLinkedDeque add()用法及代码示例
 - Java ConcurrentLinkedDeque removeFirstOccurrence()用法及代码示例
 - Java ConcurrentLinkedDeque removeLast()用法及代码示例
 - Java ConcurrentLinkedDeque hashCode()用法及代码示例
 - Java ConcurrentLinkedDeque contains()用法及代码示例
 
注:本文由纯净天空筛选整理自 Java ConcurrentLinkedQueue retainAll() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
