Java Tutorial

Collection Framework

Collection framework is the standardized mechanism of grouping of similar or dissimilar type of objects into a single object. This single object is known as collection framework object.

Collection Framework

Goals of collection frameworks:

  1. Collection framework improves the performance of JAVA, J2EE projects (when we want to transfer the bulk amount of data from client to server and server to client, using collection framework we can transfer that entire data at a time).
  2. Collection framework allows us to prove similar or dissimilar type of objects.
  3. Collection framework is dynamic in nature i.e., they are extends (arrays contains the size which is fixed in nature and they allows similar type of data).
  4. Collection framework contains adaptability feature (adaptability is the process of adding one collection object at the end of another collection object).
  5. Collection framework is algorithmic oriented (collection framework contains various sorting and searching techniques of data structures as a predefined concepts).
  6. In order to deal with collection framework we must import a package called java.util.*

Collection framework is divided into two categories. They are new collection framework and legacy (old) collection framework.

NEW COLLECTION FRAMEWORK:

New collection framework is again broadly divided into two categories. They are one dimensional collection framework and two dimensional collection framework.

One dimensional collection framework:

A one dimensional collection framework is one in which the data is organized either in the form of row or in the form of column by containing similar or dissimilar categories of objects into a single object. This single object is known as one dimensional collection framework object. One dimensional collection framework contains one dimensional collection framework interfaces and one dimensional collection framework classes.

a) One dimensional collection framework interfaces:

As a part of one dimensional collection framework interfaces in JAVA we have five interfaces. They are collection, list, set, sorted set and queue.

one dimensional collection framework

java.util.Collection:

Collection is an interface whose object allows us to organize similar or different type of objects into single object. The Collection interface is having the following features:

  1. It is available at the top of the hierarchy of all the interfaces which are available in the collection framework.
  2. An object of Collection allows us to add duplicate elements.
  3. Collection object always displays the data in forward direction only.
  4. Collection object will print the data on the console in random order.
  5. Collection object always allows us to insert an element at end only i.e., we cannot insert an element at the specific position.

Methods inCollection interface:

1. public int size (): This method is used for determining the number of elements in Collection interface object.

2. public boolean add (java.lang.Object): This method is used for adding an object to Collection object. When we use this method to add the object to Collection objects and List object, this method always returns true. Since, Collection object and List object allows duplicates. When we apply the same method with Set and SortedSet methods and when we are trying to add duplicates, this method returns false.

Note: Whatever the data we want to add through collection framework object that fundamental data must be converted into the corresponding wrapper class object and all the objects of wrapper classes are treated as objects of java.lang.Object class.

3. public boolean addAll (Collection):This method is used for adding the entire collection object at the end of another Collection object. As long as we apply this method with List and Collection interfaces, it returns true. Since, they allow duplicates. When we apply this method with Set and SortedSet, this method may return false. Since, duplicates are not allowed.

Note: Collection framework is nothing but assembling different or similar type of objects and dissembling similar or different type of objects and it is shown in the following diagrammatic representation.

boolean add all

4. public boolean isEmpty(): Returns true when there are no object found in Collection interface object otherwise return false.

5. public Object []toArray ():This method is used for extracting the data from Collection object in the form of array of objects of java.lang.Object class.

For Example:

int s=0;
Object obj []=s.toArray ();
for (int i=0; i<obj.length; i++)
{
    Interger io= (Integer) obj [i]; 
    int x=io.intValue ();
    s=s+x;
}
System.out.println ("Sum ="+s);

6. public Iterator iterator ():This method is used for extracting the data from Collection framework object.

For Example:

Iterator itr=co.iterator (); 
Int s=0;
While (itr.hasNext ())
{
    Object obj=itr.next (); 
    Integer io= (Integer) obj; 
    int x=io.intValue (); 
    s=s+x;
}

Iterator interface: Iterator is an interface which always uses the extract the data from any Collection object.

Methods inIterator interface:

1.	public boolean hasNext ()
2.	public Object next ()
3.	public Object remove ()

Method 1 is used for checking whether we have next element or not. If next element available in Collection object it returns true otherwise it returns false. Method 2 is used for obtaining the next element in the Collection object. Method 3 is used for removing the element from Collection object. Methods 2 and 3 can be used as long as method 1 returns true.

One dimentional collection framework classes:

One dimensional collection framework classes are defining all the methods of collection framework interfaces. The following table gives the collection framework classes and its hierarchy.

InterfacesClasses
CollectionAbstractCollection implements Collection
ListAbstractList extends AbstractCollection implements List
SetAbstractSet extends AbstractCollection implements Set
SortedSetAbstractSequentialList extends AbstractList implements SortedSet
LinkedList extends AbstractSequential
ArrayList extends AbstractSequential
HashSet extends AbstractSet
TreeSet extends AbstractSet

Collection framework classes contains the definition for those methods which are coming from Collection interfaces i.e., all Collection interface methods defined in collection classes AbstractCollection implements Collection, AbstractList extends AbstractCollection implements List, AbstractSet extends AbstractCollection implements Set, AbstractSequentialList extends AbstractList implements SortedSet, LinkedList extends AbstractSequential, ArrayList extends AbstractSequential, HashSet extends AbstractSet and TreeSet extends AbstractSet.

The classes AbstractCollection implements Collection, AbstractList extends AbstractCollection implements List, AbstractSet extends AbstractCollection implements Set and AbstractSequentialList extends AbstractList implements SortedSet are abstract classes we never make use of them as a part of real time applications but we make use of the classes LinkedList extends AbstractSequential, ArrayList extends AbstractSequential, HashSet extends AbstractSet and TreeSet extends AbstractSet.