Online Java Compiler By
JavaTpoint.com
import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.concurrent.ConcurrentLinkedQueue; public class JavaCollectionRetainAllExample2 { public static void main(String[] args) { Collection
collection = new ConcurrentLinkedQueue
(); List
list = new ArrayList
(); for (int i=1;i<21;i++){ collection.add(i); } System.out.println("Total no : "+ collection); for (int i=1;i<21;i++) { int j=i%2; if (j == 0) { list.add(i); } } //retains only those elements in the collection which are present in list collection.retainAll(list); System.out.println(" Table of 2 : "+collection); } }
Output