- The java collections Framework defines several classes and interfaces which can be used to represent a group of individual object as a single entity.
- These classes and interface implement the collection data structures.
- For Example : list, stack, queue or maps.
- The collection framework was designed and developed primarily by Joshua Bloch
- It was introduced in JDK 1.2
- Why to use these collections?
- We don’t have to implement every algorithm and data structure from the scratch, its already written into java.
- It is already been tested.
- Almost all Collections are derived from the java.utils.Collection interface
- The main advantage of Collections over array is that collections are grow-able in size, i.e we need not know the size of elements in advance.
- toArray() method can transform any collection into one dimensional array.
- The Collection interface extends the java.lang.Iterable interface, this is why we can use for-each loop.
We write for each loop like below example :
for(String fruits : listOfFruits){ System.out.println(fruits); }
After compilation of .java file, .class file contains below code :
for (Iterator<String> i = listOfFruits.iterator(); i.hasNext();) { String fruits = i.next(); System.out.println(fruits); }

Arrays | Collections |
---|---|
Fixed in Size | Grow-able in size |
Can hold primitive, non primitive and object data type. | Can hold only non primitive and object data type. |
No underlying Data structure, hence no ready-made method support | Implemented base on standard data structure hence for every collection ready-made collection support is available. |
Collection Interface
- Collection interface defines the most common methods which are applicable for any Collection object.
- In general Collection interface is considered as root interface of collection framework.
- Collection Interface extends Iterable Interface.
- There is no concrete class which implements collection interface directly.
Collection Interface | Collections Class |
---|---|
If you want to represent a group of individual objects we should use collection Interface | Collections is a utility class (java.utils.package) which defines several utility methods like sorting,searching for collection object. |
Methods of Collection Interface :
boolean add(E e);
boolean addAll(Collection<? extends E> c);
boolean remove(Object o);
boolean removeAll(Collection<?> c);
void clear();
boolean retainAll(Collection<?> c);
boolean contains(Object o);
boolean containsAll(Collection<?> c);
int size();
boolean isEmpty();
Iterator<E> iterator();
Object[] toArray();
boolean equals(Object o);
int hashCode();