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 JavaCollectionRetainAllExample5 { public static void main(String[] args) { Collection
collection = new ConcurrentLinkedQueue
(); int i; for(i=1;i<=50;++i){ collection.add(i); } System.out.println("collection : "+collection); List
list = new ArrayList
(); for ( i=1; i<=50;++i){ if(i%5==0){ list.add(i); } } //retains only those elements in the collection which are present in list collection.retainAll(list); System.out.println("Vowels : "+collection); } }
Output