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 JavaCollectionRetainAllExample4 { public static void main(String[] args) { Collection
collection = new ConcurrentLinkedQueue
(); char c; for(c='A';c<='Z';++c){ // collection does not support null elements collection.add(c); } 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