Collections are nothing but a natural group of items. The collections provide methods to store, retrieve, remove and manipulate the data. Various concrete classes those implements different Collection interfaces, provides implementation of algorithms for efficient operations on the group of data. The collection interfaces in the Java Collections Framework include: Collection, List, Set, Map, and so on. And the major classes in this framework are: ArrayList, LinkedList, HashSet, HashMap. The Java Collections Framework is contained inside the java.util.Collection package. Collection framework was not part of original Java release. Collections was added to J2SE 1.2. Prior to Java 2, Java provided adhoc classes such as Dictionary, Vector, Stack and Properties to store and manipulate groups of objects. Collection framework provides many important classes and interfaces to collect and organize group of alike objects.
To iterate over a collection, you need an Iterator. The java.util.Iterator package contains methods such as hasNext(), next() and remove(). The hasNext() method returns true if there are more elements in the collection. The next() method returns the next element in the collection. The remove() method removes the last element from a collection.
Some of the benefits of collections framework are:
- Reduced development effort by using core collection classes rather than implementing our own collection classes.
- Code quality is enhanced with the use of well tested collections framework classes.
- Reduced effort for code maintenance by using collection classes shipped with JDK.
- Reusability and Interoperability
Following list describes the core collection interfaces.
Methods of Collection interface
Below are the methods declared in the Collection interface.
|public boolean add(object o)||this method is used to insert an element in this collection.|
|public boolean addAll(collection c)||this method is used to insert the specified collection elements in the invoking collection.|
|public boolean remove(object o)||this method is used to delete an element from this collection.|
|public boolean removeAll(Collection c)||this method is used to delete all the elements of specified collection from the invoking collection.|
|public boolean retainAll(Collection c)||this method is used to delete all the elements of invoking collection except the specified collection.|
|public int size()||this method return the total number of elements in the collection.|
|public void clear()||this method removes the total no of element from the collection.|
|public boolean contains(object o)||this method is used to search an element.|
|public boolean containsAll(collection c)||this method is used to search the specified collection in this collection.|
|public Iterator iterator()||this method returns an iterator.|
|public Object toArray()||this method is used to converts collection into array|
|public Object toArray(Object array[ ])||this method is used to converts collection into array only for those collection elements whose type matches that of array.|
|public boolean equals(Object element)||this method is used to matches two collection and retrun true if the invoking collection and obj are equal. Otherwise, returns false.|
|public boolean isEmpty()||this method is used to checks if collection is empty or not and returns true if the invoking collection is empty. Otherwise, returns false.|
|public int hashCode()||this method is used to returns the hashcode number for invoking collection.|
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.
|public boolean hasNext()||this method returns true if iterator has more elements.|
|public object next()||this method returns the element and moves the cursor pointer to the next element.|
|public void remove()||this method removes the last elements returned by the iterator. It is rarely used.|
Collection Framework Classes and Interfaces:
- ArrayList class
- LinkedList class
- ListIterator interface
- HashSet class
- LinkedHashSet class
- TreeSet class
- PriorityQueue class
- Map interface
- HashMap class
- LinkedHashMap class
- TreeMap class
- Hashtable class
- Comparable interface
- Comparator interface