Before discussion Collection Framework first we discuss why collection in java.
Disadvantages of Array
Array is fixed.
Similar data collection.
Underlying (non standard data structure).
An array is an indexed collection of fixed number of homogeneous data elements.
The main advantages of an array are we can represent multiple values with single variable so that reliability of code will be improved.
Array is fixed in size. Once we can create an array with same size, there is no chance of increasing or decreasing size based on our requirement. Hence to use the array, we should know size in advance. But which may not be possible in advance always.
Array can hold homogeneous data elements
Student s[ ] = new Student;
s = new Student("Raju");
s = new Customer("Ram"); // CE :- Incompatible type
But we can resolve this by using Object array.
Object o[ ] = new Object;
o = new Student("Raju");
o = new Customer("Ram");
o = 10;
Arrays concept is not implemented based on some standard Data Structure. Hence readymade method support is not available for every requirement. We have to write a code every time. We have to code explicitly which increases complexity of program. To overcome above problems we should go for Collection.
Collection Framework Hierarchy
If we want to represent a group of individual object as a single entity then we should go for collection. Collection Interface defines the most common methods which are applicable for any collection object.
boolean add(Object o): Appends the specified element to the end of this list.
boolean addAll(Collection c): Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.
boolean remove(Object o): Removes the first occurrence of the specified element from the list, if it is present. If the list does not contain the element, it is unchanged.
boolean removeAll(Collection c): Removes from the list all of its elements that are contained in the specified collection.
Removes all of the elements from the list. The list will be empty after this call returns.
boolean retainAll(Collection c): Retains only the elements in the list that are contained in the specified collection. In other words, removes from the list all of its elements that are not contained in the specified collection.
boolean contains(Object o): Returns true if the list contains the specified element.
boolean containsAll(Collection c): Returns true if the collection contains all of the elements in the specified collection.
boolean isEmpty(): Returns true if the list contains no elements.
int size(): Returns the number of elements in this list.
Object toArray(): Returns an array containing all of the elements in the list in proper sequence (from first to last element).
Iterator iterator(): Returns an iterator over the elements in the list in proper sequence.
There is no current class which implements Collection interface directly.