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.
Goals of collection frameworks:
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.
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:
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.
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.
Interfaces | Classes |
---|---|
Collection | AbstractCollection implements Collection |
List | AbstractList extends AbstractCollection implements List |
Set | AbstractSet extends AbstractCollection implements Set |
SortedSet | AbstractSequentialList 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.