Need of collection frame work?

Usually we declare the variables according to our requirement. But it is difficult to declare huge number of values. So we shifted to arrays.

For eg.  Student[]s= new student[1000];

0 1 2 999

The biggest advantage of arrays is huge number of value can be stored in one variable.

Arrays have some limitations:-

  1. Arrays are fixed in size.
  2. Arrays can hold homogenous type of datatype. If we want to store any type of object then we to create object type of array.

For eg. Object a=new object[1000]

a[0]= new student();

a[1]= new customer();

  1. Arrays are not implemented based on some standard datasturcture. So no predefined functions are there for arrays.

To overcome all these problems we have to go to collections.


 Collection is a group of individual object as a single entity 
  1. Collection are growable in nature.
  2. Can hold both homogeneous and heterogeneous object.
  3. Every collection class is implemented on standard data structure .Hence readymade method support is available.

Difference between arrays and collection

Arrays Collection
arrays are fixed in size Growable in nature
With respect to memory arrays are not recommended to use With respect to memory collections are recommended to use
Performance wise good performance wise not good
Only homogenous data types can hold both homogeneous and heterogeneous data type.
Underlying Data structures is not available Standard data structure is available.
Arrays can hold primitive datatype and object can be hold It hold only objects but not primitive datatype


Collection framework:- defines several classes and interfaces to represent a group of objects as a single entity.

Difference between collection and collections

  • Collection:- it is an interface.
  • Collections:- it is a class defined in java.util package to define several utility methods like sorting, searching for collection objects.

Note:- there is no concrete class which implements collection interface directly.

Collection Hierarchy(Click)



9 key interfaces of collection framework:-

  1. Collection (Interface):- root interface of the collection framework. Defines most common method which is applicable for any collection object.
  2. List (introducduced in java 1.2v) interface:- it is the child interface of the collection. ArrayList, LinkedList, Vector, Stack are the implementing classes. Vector is called as the Legacy class. If we want to represent a group of individual objects as a single entity where duplicates are allowed and insertion order is preserved then we should go for List.
  3. Set(Interface):-Child interface of collection. Duplicates are not allowed. Insertion order is not preserved in the set.
  4. Sorted set(Interface):-child interface of set. If we want to represent a group of individual object as a single entity where duplicates are not allowed but all objects should be inserted according to some sorting order then we should go for sorted set.
  5. Navigable set(Interface):- child interface of Sorted set ,defines several methods for navigation purpose. Implementing class is TreeSet.

Note:- Vector and Stack classes are re-engineer in 1.2v to implement List interface.

Set6. Queue (Interface):- Child interface of collection. If we want to represent a group of individual object prior to processing then we should go for queue.

For Eg:- Before sending a mail, all mail ids we have to saved in the same order mails should be delivered.(FIRST IN FIRSTOUT). For this requirement queue concept is the best choice.


The Queue implementations are grouped into general-purpose and concurrent implementations(will see in later post related to Concurrency package)

General-Purpose Queue Implementations

LinkedList implements the Queue interface, providing first in, first out (FIFO) queue operations for add, poll, and so on.

The PriorityQueue class is a priority queue based on the heap data structure. This queue orders elements according to the order specified at construction time, which can be the elements’ natural ordering or the ordering imposed by an explicit Comparator.

The queue retrieval operations — poll, remove, peek, and element — access the element at the head of the queue. The head of the queue is the least element with respect to the specified ordering. If multiple elements are tied for least value, the head is one of those elements; ties are broken arbitrarily.

PriorityQueue and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The iterator provided in method iterator is not guaranteed to traverse the elements of the PriorityQueue in any particular order. For ordered traversal, consider using Arrays.sort(pq.toArray()).

Note:-all the above interfaces (Collection, List, Set, SortedSet, NavigableSet and Queue) meant for representing a group of individual object.

  1. Map Interface:-Map is not the child interface of collection . if we want to represent a group of individual objects as key value pair then we should go for map.

For eg:-

Roll No Name
101 Shourya
102 Shashank
103 Shailesh

Both key and values are objects. Duplicate keys are not allowed but values can be duplicate.


Map Click Large Map :

  1. SortedMap(interface):- Child interface of Map. If we want to represent a group of key value pair according to some sorting order of keys then we should go for sorted Map.
  2. Navigable Map(interface):- Child interface of Sorted defines several utility methods for navigation purpose.


1. If we want to represent a group of individual object as a single entity then we should go for collection.
2. In general collection interface is considered as root interface of Collection Frame work.
3. Collection interface defines the most common methods which are applicable for any collection object.

1. boolean add(object o)
2. boolean addall(Collection c)
3. boolean remove (object o)
4. boolean removeall(Collection c)
5. void clear()
6. boolean retainall(Collection c)
7. Boolean isEmpty()
8. int size()
9. boolean contains(object o)
10. boolean containsall(Collection c)
Note:- Collection interface does not contain any method to get.
List Interface
1. It is the child interface of collection
2. If we want to represent a group of individual object as single entity where duplicates are allowed and insertion order must be preserved then we should go for list
3. We can differentiate duplicates by using index.
4. We can preserve insertion order by using index, hence index play a very important role in Lit interface.
List Interface Specific methods

1. Void add(int index, object o)
2. boolean addall(int index,Collection c)
3. object get(int index)
4. object remove(int index)
5. object set(int index,object new)
6. int indexOf(object o)
7. int lastindexOf(object o)
8. ListIterator listiterator();

Implementing class of List
1. Arraylist
• The underlined data structure is resizable array or growable array.
• Duplicates are allowed
• Insertion order is preserved
• Heterogeneous objects are allowed .(except TreeSet and TreeMap everywhere heterogenous objects are allowed).
• Null insertion is possible
1. ArrayList l=new ArrayList(); Creates an empty arraylist object with default intial capacity 10. Once ArrayList reaches its maximum capacity a new arrayList will be created with new capacity=(current capacity*3/2)+1
2. ArrayList l=new ArrayList(int initialcapacity);
3. ArrayList l=new ArrayList(Collection c); meant for interconvertion between collection



[A,10,A,Null] [A,10,Null] [A,10,N,Null,s]

• Usually we can use collection to hold and transfer objects from one place to another place, to provide support for this requirement every collection already implements Serializable and Cloneable interface
• ArrayList and Vector classes implements RandomAccess interface so that we can access any Random element with the same speed.
• Hence if our frequent operation is retrieval operation then ArrayList is the best choice as Arraylist implements RandomAccess interface.
• Present in java.Util pacakage
• It does not contain any methods and it is a marker interface. Marker interface are those interface who do not have any method within themselves and tells the JVM to handle it in a particular way.

• ArrayList is the worst choice if our frequent operation is insertion or deletion in the middle.
How to get Synchronized version of ArrayList object?
By default ArrayList object is non-Synchronized but we can get Synchronized version of ArrayList by using collections class synchronizedList()method. Public static List synchronizedList(List l);

List l= collections. synchronizedList(l);

Comments are closed