Why Collection Framework?
Collections are
nothing but group of objects stored in well-defined manner. Earlier,
Arrays are used to represent these group of objects. But, arrays are not
re-sizable. size of the arrays are fixed. Size of the arrays cannot be changed
once they are defined. This causes lots of problem while handling group of
objects. To overcome this drawback of arrays, Collection framework or
simply collections are introduced in java from JDK 1.2.
What is Collection
Framework in Java?
Collection
Framework is representing a unified architecture for storing and manipulating the
group of objects. Using collection framework, you can store the objects as a list or
as a set or as a queue or as a map and
perform operations like adding an object or removing an object or sorting
the objects without much hard work.
Class
Hierarchy of Collection Framework:
The
entire collection framework is divided into four interfaces.
1)
List —>
It handles sequential list of objects. ArrayList, Vector and
LinkedList classes implement this interface.
2)
Queue —> It handles special list of objects in
which elements are removed only from the head. LinkedList and PriorityQueue classes
implement this interface.
3)
Set —>
It handles list of objects which must contain unique element. This
interface is implemented by HashSet and LinkedHashSet classes
and extended by SortedSet interface which in turn, is
implemented by TreeSet.
4)
Map —>
This is the one interface in Collection Framework which is not inherited from
Collection interface. It handles group of objects as Key/Value pairs. It is
implemented by HashMap and HashTable classes
and extended by SortedMap interface which in turn is
implemented by TreeMap.
Three
of above interfaces (List, Queue and Set) inherit from Collection
interface. Although, Map is included in collection framework it does not
inherit from Collection interface.
Collection
interface is the root level interface in the
collection framework. List, Queue and Set are all sub interfaces of Collection
interface. JDK does not provide any direct implementations of this interface.
But, JDK provides direct implementations of its sub interfaces.
Collection
interface extends Iterable interface which is a member of
java.lang package. Iterable interface has only one method called iterator(). It
returns an Iterator object, using that object you can iterate over the elements
of Collection. Here is the class diagram of Collection interface.
Collection
interface contains total 15 abstract methods. 14 of its own and one is
inherited from Iterable interface. Here is the list and descriptions of those
methods.
No.
|
Method
|
Description
|
1
|
public
boolean add(Object element)
|
is
used to insert an element in this collection.
|
2
|
public
boolean addAll(Collection c)
|
is
used to insert the specified collection elements in the invoking collection.
|
3
|
public
boolean remove(Object element)
|
is
used to delete an element from this collection.
|
4
|
public
boolean removeAll(Collection c)
|
is
used to delete all the elements of specified collection from the invoking
collection.
|
5
|
public
boolean retainAll(Collection c)
|
is
used to delete all the elements of invoking collection except the specified
collection.
|
6
|
public
int size()
|
return
the total number of elements in the collection.
|
7
|
public
void clear()
|
removes
the total no of element from the collection.
|
8
|
public
boolean contains(Object element)
|
is
used to search an element.
|
9
|
public
boolean containsAll(Collection c)
|
is
used to search the specified collection in this collection.
|
10
|
public
Iterator iterator()
|
returns
an iterator.
|
11
|
public
Object[] toArray()
|
converts
collection into array.
|
12
|
public
boolean isEmpty()
|
checks
if collection is empty.
|
13
|
public
boolean equals(Object element)
|
matches
two collection.
|
14
|
public
int hashCode()
|
returns
the hashcode number for collection.
|
Iterator interface
Iterator
interface provides the facility of iterating the elements in forward
direction only.
|
Methods
of Iterator interface
There
are only three methods in the Iterator interface. They are:
- public boolean
hasNext() it returns true if iterator has
more elements.
- public object next() it
returns the element and moves the cursor pointer to the next element.
- public void remove() it
removes the last elements returned by the iterator. It is rarely used.
Note
:
equals() and hashcode() methods in the Collection
interface are not the methods of java.lang.Object class. Because, interfaces do
not inherit from Object class. Only classes in java are sub classes of Object
class. Any classes implementing Collection interface must provide their own
version of equals() and hashcode() methods or they can retain default version
inherited from Object class.
No comments:
Post a Comment