Collections in java is standard way to handle the group of objects in java program. it was introduced in java 2, prior to that Dictionary, Vector, Stack, and Properties classes are used to store and manipulate groups of objects. These classes are good and useful but lacks a standard approach. Implementation of these classes are completely different to each other thus implementation of custom classes around it is not easy.
Collection framework was designed to meet several goals like..
- Framework had to be very efficient.
- Framework had to allow different types of collections to work in a similar manner.
- Creating custom Classes and Interfaces should be easy.
- Interfaces and Classes : The entire collections framework is designed around a set of standard interfaces like List, Set, Queue & so on. It contains standard implementation of classes like ArrayList, LinkedList etc.
- Algorithms : Algorithms operate on collections and provide a standard means of manipulating collections. Thus, algorithms are available for all collections.
Implementation of Collection Framwork
Methods of Collection Interface.
Followings are the methods declared under Collection interface.
|boolean add(Object obj)||Adds obj to the invoking collection. Returns true if obj was added to the collection. Returns false if obj is already a member of the collection, or if the collection does not allow duplicates.|
|boolean addAll(Collection c)||Adds all the elements of collection c to the invoking collection. Returns true if the operation succeeded. Otherwise, returns false.|
|boolean contains(Object obj)||Returns true if obj is an element of the invoking collection. Otherwise, returns false.|
|boolean containsAll(Collection c)||Returns true if the invoking collection contains all elements of c.Otherwise,returns false.|
|boolean equals(Object obj)||Returns true if the invoking collection and obj are equal. Otherwise, returns false.|
|boolean isEmpty()||Returns true if the invoking collection is empty. Otherwise, returns false.|
|boolean remove(Object obj)||Removes one instance of obj from the invoking collection. Returns true if the element was removed. Otherwise, returns false.|
|boolean removeAll(Collection c)||Removes all elements of c from the invoking collection. Returns true if the collection changed (i.e., elements were removed). Otherwise, returns false.|
|boolean retainAll(Collection c)||Removes all elements from the invoking collection except those in c. Returns true if the collection changed (i.e., elements were removed). Otherwise, returns false.|
|void clear()||Removes all elements from the invoking collection.|
|int hashCode()||Returns the hash code for the invoking collection.|
|Iterator iterator()||Returns an iterator for the invoking collection.|
|int size()||Returns the number of elements held in the invoking collection.|
|Object toArray()||Returns an array that contains all the elements stored in the invoking collection. The array elements are copies of the collection elements.|
|Object toArray(Object array)||Returns an array containing only those collection elements whose type matches that of array. The array elements are copies of the collection elements. If the size of array equals the number of matching elements, these are returned in array. If the size of array is less than the number of matching elements, a new array of the necessary size is allocated and returned. If the size of array is greater than the number of matching elements, the array element following the last collection element is set to null. An ArrayStoreException is thrown if any collection element has a type that is not a subtype of array.|