wellnwill Marathahalli Bangalore

Collection Framework

Collection represents group of similar or different type of objects into a single object.

This single object is known as collection object. In collection, there are so many predefined methods available to perform operations like searching, sorting, adding, removing etc.

Object member Hashing algorithm Array Collection
The size of an array is fixed. Once we create an array with specified size, the size cannot be changed. Collections are dynamic in nature i.e. based on the requirement the size of collection will be changed.
Array can hold similar type of data elements. Collection can hold homogeneous and heterogeneous type of data elements.
Predefined methods are not available to perform operations on an array. Predefined methods are available to perform operations on collection.
Arrays can store both primitive and object. Collection can store only object.

Collection Framework

Collection framework represents group of classes and interfaces which can be used to represent a group of similar and non-similar type of object as a single entity and provides readymade methods support to store and manipulate collection objects.

Collection API is present in java.util package. Collection’s subclasses are used to manage the group of elements without key and value pair.

I - Interface

c - class

Methods of Collection interface

public boolean add(Object ob):

This method is used to add an object to collection object and List object. This method returns true when we add elements for Collection and List object because they allow duplicate elements.

This method returns false when we add elements for Set and SortedSet object if the new element is duplicate element.Set and SortedSet object does not allow duplicates.

public boolean addAll(Collection c):

This method is used to add all the elements of specified collection object at the end of current collection because insertion order is preserved in case of collection and List.

This method returns true when we add elements for Collection and List object because they allow duplicate elements.

This method returns false when we add elements for Set and SortedSet object if the new element is duplicate element.Set and SortedSet object does not allow duplicates.

public int size():

This methods returns the number of elements present in Collection object.

public boolean isEmpty():

This method checks whether collection is empty or not. It returns true when Collection object is empty otherwise return false.

public boolean contains(object ob):

This method checks whether given object is present in collection or not.

public boolean containsAll(Collection c):

This method checks whether current collection contains all the elements of specified collection.

ppublic boolean remove(Object ob):

This method is used to remove first existence of specified object from collection.

public boolean removeAll(Collection c):

This method is used to remove similar elements of specified collection from current collection.

public boolean retainAll(Collection c):

This method is used to remove dissimilar elements of specified collection from current collection.

public boolean clear():

This method is used to clear all the elements from collection.

public object[] toArray():

This method is used to convert collection objects into array elements.

public boolean equals(Object obj):

This method is used to compare two collection objects.

public Iterator iterator():

This method returns Iterator object that points before the first record.

Iterator interface:

Iterator interface provides the facility for traversing the collection objects in forward direction. Iterator interface has three methods.

public boolean hasNext():

This method is used to check whether next element is available or not in the collection object. If next element present in the collection object then it returns true otherwise it returns false.

public Object next():

This method is used to retrieve the next element in the collection object.

public void remove():

This method is used to remove the current element from collection object.

                                                      
import java.util.*;
class Test231{
public static void main(String args[]){
Collection c=new ArrayList();
//adding elements into Collection
c.add("Pune");
c.add("Lucknow");
c.add("Mysore");
c.add("Bangalore");
System.out.println(c);//[Pune,Lucknow,mysore,bangalore]
System.out.println("Size of collection:"+c.size());//4
System.out.println("Empty :"+c.isEmpty());
Object obj[]=c.toArray();
for(Object temp:obj)
{
System.out.println(temp);
}
System.out.println(c.contains("Pune"));//true
System.out.println(c.remove("Pune"));//true
System.out.println(c.remove("Mangalore"));//false
System.out.println(c);//[Lucknow,mysore,bangalore]
c.clear();
System.out.println(c);//[]
}
}
                                                      
import java.util.*;
class Test232{
public static void main(String args[]){
Collection c=new ArrayList();
//adding elements into Collection
c.add("Pune");
c.add("Lucknow");
c.add("Mysore");
c.add("Bangalore");
System.out.println(c);//[Pune,Lucknow,Mysore,bangalore]
Iterator itr=c.iterator();
while(itr.hasNext())
{
 Object obj=itr.next();
 System.out.println(obj);
 if(obj.equals("Mysore"))
 {
  itr.remove();
 }
}
System.out.println(c); //[Pune,Lucknow, bangalore]
}
}
                                                      
import java.util.*;
class Test233{
public static void main(String args[]){
Collection c=new ArrayList();
c.add("Pune");
c.add("Lucknow");
c.add("Mysore");
c.add("Bangalore");
System.out.println(c);//[Pune,Lucknow,mysore,bangalore]
Collection c1=new ArrayList();
c1.add("Pune1");
c1.add("Lucknow1");
c1.add("Mysore1");
c1.add("Bangalore1");
System.out.println(c1);
System.out.println(c.addAll(c1));
System.out.println(c);
}
}
                                                      
import java.util.*;
class Test234{
public static void main(String args[]){
Collection c=new ArrayList();
c.add("Pune");
c.add("Lucknow");
c.add("Mysore");
c.add("Bangalore");
System.out.println(c);//[Pune,Lucknow,mysore,bangalore]
Collection c1=new ArrayList();
c1.add("Pune1");
c1.add("Lucknow1");
c1.add("Mysore1");
c1.add("Bangalore1");
System.out.println(c1);
System.out.println(c.contains("Mumbai"));//false
System.out.println(c.contains("Bangalore"));//true
System.out.println(c.containsAll(c1));//false
}
}
                                                      
import java.util.*;
class Test235{
public static void main(String args[]){
Collection c=new ArrayList();
c.add("Pune");
c.add("Lucknow");
System.out.println(c);//[Pune,Lucknow]
Collection c1=new ArrayList();
c1.add("Pune");
c1.add("Lucknow");
c1.add("Pune1");
c1.add("Lucknow1");
System.out.println(c1); //[Pune,Lucknow, Pune1,Lucknow1]                                                      
System.out.println(c.remove("Mumbai"));//false
System.out.println(c.remove("Pune"));//true
System.out.println(c);//[Lucknow]
System.out.println(c.removeAll(c1));//true
System.out.println(c);//[]
System.out.println(c1); //[Pune,Lucknow, Pune1,Lucknow1]
c.add("Kerala");
System.out.println(c.retainAll(c1));//true
System.out.println(c);//[]
}
}
                                                      
import java.util.*;
class Test0234
{
public static void main(String args[]){
Collection c=new ArrayList();
c.add("Pune");
c.add("Lucknow");
System.out.println(c);//[Pune,Lucknow]
Collection c1=new ArrayList();
c1.add("Pune");
c1.add("Lucknow");
c1.add("Mysore1");
c1.add("Bangalore1");
System.out.println(c1);
System.out.println(c.containsAll(c1));//false
}
}
                                                      
import java.util.*;

class Test0235
{
public static void main(String args[]){
Collection c=new ArrayList();
c.add("Pune");
c.add("Lucknow");
System.out.println(c);//[Pune,Lucknow]
Collection c1=new ArrayList();
c1.add("Pune");
c1.add("Lucknow");
System.out.println(c1);
System.out.println(c.containsAll(c1));//true
}
}
Footer with Map

Top