18-03-25 ----------- Q)Example program on methods of Collection interface. //CollectionInterfaceExample.java import java.util.*; class CollectionInterfaceExample{ public static Collection getCollection(){ return new ArrayList();//list based collection object created and returned }//user defined method public static void main(String[] args){ Collection collection1=getCollection();//step 1:- creating collection object System.out.println(collection1.getClass().getName()+" class object created"); collection1.add(10); collection1.add(13); collection1.add(14); collection1.add(15);//step 2:- add elements to the collection System.out.println(collection1); Collection collection2=getCollection(); collection2.addAll(collection1); System.out.println(collection2); System.out.println("Is 14 present in collection 1:"+collection1.contains(14)); System.out.println("No of elements in collection 2:"+collection2.size()); collection2.clear(); System.out.println("No of elements in collection 2:"+collection2.size()); for(int element:collection1){ System.out.println(element); }//1 way of iterating over a collection Iterator i=collection1.iterator();//cursor object returned. while(i.hasNext()){ System.out.println(i.next()); }//2nd way of iterating a collection System.out.println(" Is 13 removed from the collection :"+collection1.remove(13)); System.out.println(collection1); collection1.clear(); Collections.addAll(collection1,10,11,12); Collections.addAll(collection2,11,12); collection1.removeAll(collection2); System.out.println(collection1); } } Q)Explain about a list based collection. =>A collection object that implements java.util.List interface is nothing but a list based collection. =>A list based collection is also known as an ordered or indexed collection. =>In a list based collection; insertion order is preserved,duplicate elements are allowed and nulls can be inserted.