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 JavaCollectionRetainAllExample1 { public static void main(String[] args) { Collection
collection = new ConcurrentLinkedQueue
(); char i; for(i='a';i<='z';++i){ collection.add(i); } System.out.println("collection : "+collection); List
list = new ArrayList
(); list.add('a'); list.add('e'); list.add('i'); list.add('o'); list.add('u'); //retains only those elements in the collection which are present in list collection.retainAll(list); System.out.println("Vowels : "+collection); } }
Output