Close

Java Collections - ArrayBlockingQueue.retainAll() Examples

Java Collections Java Java API 


Class:

java.util.concurrent.ArrayBlockingQueue

java.lang.Objectjava.lang.Objectjava.util.AbstractCollectionjava.util.AbstractCollectionjava.util.CollectionCollectionjava.util.AbstractQueuejava.util.AbstractQueuejava.util.QueueQueuejava.util.concurrent.ArrayBlockingQueuejava.util.concurrent.ArrayBlockingQueuejava.util.concurrent.BlockingQueueBlockingQueuejava.io.SerializableSerializableLogicBig

Method:

public boolean retainAll(Collection<?> c)

Retains only the elements in this collection that are contained in the specified collection. In other words, removes from this collection all of its elements that are not contained in the specified collection.


Examples


package com.logicbig.example.arrayblockingqueue;

import java.util.Arrays;
import java.util.concurrent.ArrayBlockingQueue;

public class RetainAllExample {

public static void main(String... args) {
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(3, true, Arrays.asList(1, 3, 5));
System.out.println("queue before: " + q);
boolean b = q.retainAll(Arrays.asList(1, 5, 7));
System.out.println(b);
System.out.println("queue after: " + q);
}
}

Output

queue before: [1, 3, 5]
true
queue after: [1, 5]




See Also