The Collection Framework in Java is a collection of interfaces and classes to store, process and transfer the data efficiently.
The Collection Framework in Java is used to represent group individual objects as a single entity.
Where we use Collection Framework in Projects?
In projects, it is very common to use collection framework where ever we need to deal with group of objects. Let us look into following image which explains searching functionality of products on e-commerce website.

As per above image, User is searching for products in e-commerce website with ‘Name’, ‘Min Price’, ‘Max Price’ attributes. Then the request will go to Controller -> Service -> Repository -> Database. The database returned results we are storing into collection object, transfer(return) those collection object from repository to service, In service we are processing(filter) data as per user needs then transferring(return) to controller then transfer to UI. So for storing, processing and transferring we are using collection object.
NOTE: In the above example, in the place of List<Product> we can use array(Product[]), But arrays has size limitation and there is no pre-defined method support for data processing(search, sort, filter … etc.) in arrays.
Hence, Where ever there is a need of storing, processing, transferring group of objects then we should go for Collection Framework.
Hierarchy of Collection Framework
The java.util package contains all the classes and interfaces for the Collection framework. Let us see the hierarchy of Collection framework.

key interfaces of collection framework
Collection framework has defined several interfaces and classes to deal with different kind of data sets
The Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two main “root” interfaces of Java collection classes
The utility package, (java.util) contains all the classes and interfaces that are required by the collection framework
Iterable
- Iterable is the root interface for the entire collection framework
- The main functionality of this interface is to provide an iterator for the collections
- This interface contains only one abstract method which is the iterator() which returns Iterator object.
1 |
Iterator iterator(); |
Collection
- Collection interface extends the Iterable interface
- It declares basic methods that every Collection should have
- This interface builds a foundation for all Collection implemented classes
- There is no concrete class which implement Collection interface directly
List
- List is a child interface of the Collection interface
- Insertion order is preserved
- Duplicate objects are allowed
- List interface has methods to perform position based operations
- The position-oriented operations include the ability to insert an element, get an element, as well as remove or change an element at the desired position.
- List interface is implemented by classes ArrayList, LinkedList, Vector, and Stack
We can instantiate the List object with any of it’s implemented classes as follows.
1 2 3 4 5 |
List <Data-Type> list1 = new ArrayList<>(); List <Data-Type> list2 = new LinkedList<>(); List <Data-Type> list3 = new Vector<>(); List <Data-Type> list4 = new Stack<>(); // Data-Type can be any Java Type |

ArrayList
- ArrayList class implements the List interface
- ArrayList underlying data structure is resizable array or grow able array
- Insertion order is preserved
- Duplicate objects are allowed
- null insertion is possible
- ArrayList class is non synchronized
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.vidvaan.corejava.collections01.overview; import java.util.ArrayList; // ArrayList example to create, add and display public class ArrayListDemo { public static void main(String[] args) { ArrayList<String> nameList = new ArrayList<String>();// Creating ArrayList nameList.add("Sekhar Reddy");// Adding String to ArrayList nameList.add("Tanvika"); nameList.add("Mounika"); System.out.println("nameList : " + nameList); // Display ArrayList } } |
Output :
nameList : [Sekhar Reddy, Tanvika, Mounika]
- Collections class defines the following method to get synchronized version of any List object
1 2 |
List<String> arrayList = new ArrayList<String>(); List<String> synchronizedArrayList = Collections.synchronizedList(arrayList); |
- ArrayList implements Serializable and Clonable interfaces to support serialization and cloning
- ArrayList and Vector implements RandomAccess interface so that we can access any element randomly with the same speed
1 2 3 4 5 6 |
ArrayList list = new ArrayList(); System.out.println(list instanceof Serializable); // true System.out.println(list instanceof Cloneable); // true System.out.println(list instanceof RandomAccess); // true System.out.println(list instanceof Collection); // true System.out.println(list instanceof List); // true |
- ArrayList is best choice, if our requirement is frequent retrieval operation
- ArrayList is the worst choice if our requirement is frequent adding or removing elements based on the index because shifting is required
LinkedList
- LinkedList class implements the List interface
- Underlying data structure is doubly linked list
- Insertion order is preserved
- Duplicate objects are allowed
- null insertion is allowed
- LinkedList class is non synchronized
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.vidvaan.corejava.collections01.overview; import java.util.LinkedList; // LinkedList example to create, add and display public class LinkedListDemo { public static void main(String[] args) { LinkedList<String> nameList = new LinkedList<String>();// Creating LinkedList nameList.add("Sekhar Reddy");// Adding String to LinkedList nameList.add("Tanvika"); nameList.add("Mounika"); System.out.println("nameList : " + nameList); // Display LinkedList } } |
Output :
nameList : [Sekhar Reddy, Tanvika, Mounika]
- Collections class defines the following method to get synchronized version of any List object
1 2 |
List<String> linkedList = new LinkedList<String>(); List<String> synchronizedLinkedList = Collections.synchronizedList(linkedList); |
- LinkedList class implements Serializable an Cloneable interfaces but not RandomAccess
1 2 3 4 5 6 |
LinkedList list = new LinkedList(); System.out.println(list instanceof Serializable); // true System.out.println(list instanceof Cloneable); // true System.out.println(list instanceof RandomAccess); // false System.out.println(list instanceof Collection); // true System.out.println(list instanceof List); // true |
- In general LinkedList class can be used for implementing Stacks and Queues. To support this type of requirement LinkedList contains the following six specific methods
- void addFirst(Object o);
- void addLast(Object o);
- Object removeFirst();
- Object removeLast()
- Object getFirst()
- Object getLast();
- LinkedList is the Best choice if our requirement is frequent insertion or deletion or modification based on the position because no shifting is required
- LinkedList worst choice if frequent operation is retrieval
Vector
- Vector class implements the List interface
- It is a legacy collection class
- Underlying data structure is Resizable array or grow able array
- Insertion order is preserved
- Duplicate objects are allowed
- null insertion is possible
- Vector class is synchronized
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.vidvaan.corejava.collections01.overview; import java.util.Vector; // Vector example to create, add and display public class VectorDemo { public static void main(String[] args) { Vector<String> namesVector = new Vector<String>();// Creating Vector namesVector.add("Sekhar Reddy");// Adding String to Vector namesVector.add("Tanvika"); namesVector.add("Mounika"); System.out.println("namesVector : " + namesVector); // Display Vector } } |
Output :
namesVector : [Sekhar Reddy, Tanvika, Mounika]
- Vector class implements Serializable, Clonable, RandomAccess, List and Collection interfaces
- Vector is similar to ArrayList. However, It is synchronized and contains many methods that are not the part of Collection framework.
- Vector is best choice, if our requirement is frequent retrieval operation
- Vector is the worst choice, if our requirement is frequent adding or removing elements based on the index because shifting is required
- Every method is synchronized hence Vector object by default thread safe
ArrayList Vs Vector
ArrayList | Vector |
---|---|
No method is synchronized | Every method is synchronized |
ArrayList is not Thread safe | Vector is thread safe |
Performance is high than Vector | Performance is low than ArrayList |
It was introduced in 1.2 version and it is non-legacy | It was introduced in 1.0 version and it is legacy |
Stack
- Stack is the child of Vector, It contains only one constructor
- Stack stack = new Stack();
- Stack follows LIFO(Last In First Out)
- The Stack contains all of the methods of Vector class and also provides its own methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.vidvaan.corejava.collections01.overview; import java.util.Stack; // Stack example to create, add and display public class StackDemo { public static void main(String[] args) { Stack<String> namesStack = new Stack<String>();// Creating Stack namesStack.push("Sekhar Reddy");// Adding String to Stack namesStack.push("Tanvika"); namesStack.push("Mounika"); System.out.println("namesStack : " + namesStack); // Display Stack // Get the elements from top of the stack System.out.println("Stack Top Element: " + namesStack.pop()); } } |
Output :
namesStack : [Sekhar Reddy, Tanvika, Mounika]
Stack Top Element: Mounika
Set
- Set is child interface of Collection
- Set doesn’t have methods, All methods are inherited from Collection
- Duplicate objects are not allowed
- Insertion order not preserved
- We can store at most one null value in Set
- Set interface implemented by classes HashSet, LinkedHashSet and TreeSet.
We can instantiate the Set object with any of it’s implemented classes as follows.
1 2 3 4 |
Set<Data-Type> set1 = new HashSet<>(); Set<Data-Type> set2 = new LinkedHashSet<>(); Set<Data-Type> set3 = new TreeSet<>(); // Data-Type can be any Java Type |
HashSet
- HashSet class implements Set interface
- HashSet underlying data structure is hashtable
- Duplicates objects are not allowed. If we are trying to insert any duplicate object we won’t get any compile time or runtime errors. But add() method simply returns false.
- Insertion order is not preserved because it is based on hashCode() of the objects
- HashSet implements Serializable and Clonable interface
- HashSet is best suitable to perform search operations
- null insertion is possible, But not advisable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.vidvaan.corejava.collections01.overview; import java.util.HashSet; import java.util.Set; //HashSet example to create, add and display public class HashSetDemo { public static void main(String[] args) { // Create HashSet Set<String> hashSet = new HashSet<>(); // Add elements to hashSet hashSet.add("CC"); hashSet.add("DD"); hashSet.add("AA"); hashSet.add("BB"); hashSet.add("ZZ"); // Display ArrayList System.out.println("HashSet elements : " + hashSet); // Add duplicate element, hashSet will ignore duplicate item boolean isAdded = hashSet.add("ZZ"); System.out.println("Duplicate 'ZZ' element added ? : " + isAdded);// false System.out.println("After dupliate added, HashSet elements : " + hashSet); } } |
Output :
HashSet elements : [CC, BB, DD, ZZ, AA]
Duplicate ‘ZZ’ element added ? : false
After dupliate added, HashSet elements : [CC, BB, DD, ZZ, AA]
LinkedHashSet
- LinkedHashSet is the child class of HashSet
- Insertion order is preserved
- Duplicates objects are not allowed
HashSet | LinkedHashSet |
---|---|
Insertion is not preserved | Insertion is preserved |
Underlying data structure is Hashtable | Underlying data structure is combination of LinkedList and HashTable |
Introduced in 1.2 version | Introduced in 1.4 version |
- In the above HashSetDemo program if we replace HashSet with LinkedHashSet, elements will be inserted in “insertion oreder”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.vidvaan.corejava.collections01.overview; import java.util.LinkedHashSet; import java.util.Set; //LinkedHashSet example to create, add and display public class HashSetDemo { public static void main(String[] args) { // Create LinkedHashSet Set<String> hashSet = new LinkedHashSet<>(); // Add elements to LinkedHashSet hashSet.add("CC"); hashSet.add("DD"); hashSet.add("AA"); hashSet.add("BB"); hashSet.add("ZZ"); // Display ArrayList System.out.println("LinkedHashSet elements : " + hashSet); // Add duplicate element, hashSet will ignore duplicate item boolean isAdded = hashSet.add("ZZ"); System.out.println("Duplicate 'ZZ' element added ? : " + isAdded);// false System.out.println("After dupliate added, LinkedHashSet elements : " + hashSet); } } |
Output :
LinkedHashSet elements : [CC, DD, AA, BB, ZZ]
Duplicate ‘ZZ’ element added ? : false
After dupliate added, LinkedHashSet elements : [CC, DD, AA, BB, ZZ]
- LinkedHashSet and LinkedHashMap classes are best suitable to implement cashing applications where duplicates are not allowed
SortedSet
- SortedSet is the child interface of Set
- SortedSet elements are arranged in some order (ascending/descending) order
- SortedSet provides the additional methods that inhibit the natural ordering of the elements
- TreeSet class implements SortedSet interface
We can instantiate the SortedSet object with any of it’s implemented classes as follows.
1 2 |
SortedSet<Data-Type> set = new TreeSet<>(); // Data-Type can be any Java Type |
NavigableSet
- NavigableSet is the child interface of SortedSet
- NavigableSet declares several methods for navigation purposes
- TreeSet class implements NavigableSet interface
We can instantiate the NavigableSet object with any of it’s implemented classes as follows.
1 2 |
NavigableSet<Data-Type> set = new TreeSet<>(); // Data-Type can be any Java Type |

TreeSet
fkjdflskfj
Queue
- Queue is the child interface of Collection
- Queue interface maintains the FIFO(first-in-first-out) order
- Queue is used to hold the elements which are about to be processed
- Queue interface implemented by classes PriorityQueue and ArrayDeque
We can instantiate the Queue object with any of it’s implemented classes as follows.
1 2 3 |
Queue<Data-Type> q1 = new PriorityQueue<>(); Queue<Data-Type> q2 = new ArrayDeque<>(); // Data-Type can be any Java Type |
Deque
- Deque is the child interface of Queue
- Deque also known as a double-ended queue
- Deque allow us to add and remove the elements from both the ends of the queue
- Deque interface implemented by class ArrayDeque
We can instantiate the Deque object with any of it’s implemented classes as follows.
1 2 |
Deque<Data-Type> q2 = new ArrayDeque<>(); // Data-Type can be any Java Type |

Map
- All the above interfaces(Collection, List, Set, SortedSet, NaviagableSet and Queue) can be used to represent group of individual objects.
- If we want to represent a group of objects as key value pairs then we should go for Map interface
- Map is not child interface of Collection
- Map keys are unique but values can be duplicate
- Map interface implemented by classes HashMap, LinkedHashMap, TreeMap, WeakHashMap, IdentityHashMap, HashTable and Properties
We can instantiate the Map object with any of it’s implemented classes as follows.
1 2 3 4 5 6 7 8 |
Map<Data-Type> map1 = new HashMap<>(); Map<Data-Type> map2 = new LinkedHashMap<>(); Map<Data-Type> map3 = new TreeMap<>(); Map<Data-Type> map4 = new WeakHashMap<>(); Map<Data-Type> map5 = new IdentityHashMap<>(); Map<Data-Type> map6 = new HashTable<>(); Map<Data-Type> map7 = new Properties<>(); // Data-Type can be any Java Type |
SortedMap
- SortedMap is the child interface of Map
- SortedMap key elements are arranged in some order (ascending/descending) order
- SortedMap provides the additional methods that inhibit the natural ordering of the key elements
- SortedMap interface implemented by class TreeMap
We can instantiate the SortedSet object with any of it’s implemented classes as follows.
1 2 |
SortedMap<Data-Type> map = new TreeMap<>(); // Data-Type can be any Java Type |
NavigableMap
- NavigableMap is the child interface of SortedMap
- NavigableMap declares several methods for key navigation purposes
- NavigableMap interface implemented by class TreeMap
We can instantiate the NavigableMap object with any of it’s implemented classes as follows.
1 2 |
NavigableMap<Data-Type> map = new TreeMap<>(); // Data-Type can be any Java Type |

Types of cursors
- If we want to retrieve the objects of a Collection one by one we should go for cursors. There are three types of Cursors available in java.
- Enumeration
- Iterator
- ListIterator
Enumeration
- Enumeration interface is unidirectional cursor
- Enumeration is applicable only for legacy collection classes(Stack, Vector, HashTable)
- We can get Enumeration object by using elements() method of Vector
- Enumeration e= vector.elements()
- Enumeration interface defines the following two methods.
- public boolean hasMoreElements()
- public Object nextElement()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.vidvaan.corejava.collections01.overview; import java.util.Enumeration; import java.util.Vector; // Enumeration example to iterate over Vector public class EnumerationDemo { public static void main(String[] args) { Vector<String> namesVector = new Vector<String>();// Creating Vector namesVector.add("Sekhar Reddy");// Adding String to Vector namesVector.add("Tanvika"); namesVector.add("Mounika"); System.out.println("namesVector : " + namesVector); // Display all elements of Vector // Access Vector elements one by one using Enumeration cursor Enumeration<String> enumeration = namesVector.elements(); while (enumeration.hasMoreElements()) { String name = enumeration.nextElement(); System.out.println("name: " + name); } } } |
Output :
namesVector : [Sekhar Reddy, Tanvika, Mounika]
name: Sekhar Reddy
name: Tanvika
name: Mounika
- Enumeration concept is applicable only for legacy classes and hence it is not a universal cursor
- While performing iteration we are not allowed to add/remove/update elements of the collection. i.e. Enumeration read only cursor
- To overcome these limitation Iterator was introduced
The implementation class of Enumeration is anonymous class
Iterator
- Iterator interface is unidirectional cursor
- Iterator is applicable for all collection classes, Hence it is universal cursor
- We can get Iterator object by using iterator() method of java.lang.Iterable interface which is root interface of all collection classes
- Iterator iterator = anyCollectionObject.iterator();
- Iterator interface defines the following three methods
- public boolean hasNext();
- public Object next();
- pulbic void remove();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.vidvaan.corejava.collections01.overview; import java.util.ArrayList; import java.util.Iterator; import java.util.List; // Iterator example to iterate over ArrayList public class IteratorDemo { public static void main(String[] args) { List<String> namesList = new ArrayList<String>();// Creating ArrayList namesList.add("Sekhar Reddy");// Adding String to ArrayList namesList.add("Tanvika"); namesList.add("Mounika"); // Display all elements of ArrayList System.out.println("namesList : " + namesList); // Access ArrayList elements one by one using Iterator cursor Iterator<String> iterator = namesList.iterator(); while (iterator.hasNext()) { String name = iterator.next(); System.out.println("name: " + name); // If required we can remove elements from collection using Iterator if (name.equals("Mounika")) { iterator.remove(); } } // Display all elements of ArrayList after removing 'Mounika' System.out.println("namesList : " + namesList); } } |
Output :
namesList : [Sekhar Reddy, Tanvika, Mounika]
name: Sekhar Reddy
name: Tanvika
name: Mounika
namesList : [Sekhar Reddy, Tanvika]
- Using remove() method of Iterator, we can remove the elements from the collection object
- While performing iteration we can perform only read and removal operations, there is no chance of performing add/modify elements in collection. To overcome these limitation ListIterator was introduced
The implementation class of Iterator is Itr, which is an inner class in AbstractList
ListIterator
- ListIterator interface is the child interface of Iterator
- ListIterator interface is bi-directional cursor
- ListIterator applicable only for List interface implemented classes
- We can get ListIterator object by using listIterator() method of List interface
- Iterator iterator = anyCollectionObject.iterator();
- Iterator interface defines the following three methods
- public boolean hasNext();
- public Object next();
- public int nextIndex();
- If there is no next element it returns size of the list but not -1.
- public boolean hasPrevious();
- public Object previous();
- public int previousIndex();
- If there is no previous element then this method returns -1.
- public void remove();
- public void add(Object newObj);
- public void set(Object newObj);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.vidvaan.corejava.collections01.overview; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; // ListIterator example to iterate over LinkedList public class ListIteratorDemo { public static void main(String[] args) { List<String> namesList = new LinkedList<String>();// Creating LinkedList namesList.add("Sekhar Reddy");// Adding String to LinkedList namesList.add("Tanvika"); namesList.add("Mounika"); // Display all elements of LinkedList System.out.println("Before Iteration namesList : " + namesList); // Access LinkedList elements one by one using Iterator cursor ListIterator<String> listIterator = namesList.listIterator(); while (listIterator.hasNext()) { String name = listIterator.next(); System.out.println("name: " + name); // If required we can remove elements from collection using ListIterator if (name.equals("Mounika")) { listIterator.remove(); } // If required we can modify elements in collection using ListIterator if (name.equals("Sekhar Reddy")) { listIterator.set("Mr. " + name); } // If required we can add elements to collection using ListIterator if (name.equals("Tanvika")) { listIterator.add("KesavaReddy"); } } // Display all elements of LinkedList after iterating and processing System.out.println("After Iteration namesList : " + namesList); } } |
Output :
Before Iteration namesList : [Sekhar Reddy, Tanvika, Mounika]
name: Sekhar Reddy
name: Tanvika
name: Mounika
After Iteration namesList : [Mr. Sekhar Reddy, Tanvika, KesavaReddy]
- While performing iteration we can perform add/remove/modify elements of List implemented classes
- It is most powerful cursor, But it is applicable for only List implemented class objects.
The implementation class of ListIterator is ListItr, which is an inner class in AbstractList
Enumeration vs Iterator vs ListIterator
Properties | Enumeration | Iterator | ListIterator |
---|---|---|---|
Is it Legacy | Yes | No | No |
It is Applicable for | Only legacy classes | Any Collection Object | Any List object |
Movement | Single direction(forward) | Single direction(forward) | Bidirectional |
How to get | legacyCollection Object.elements(); | anyCollection Object.iterator(); | listCollection Object.listIterator(); |
Accessibility | read only | read and remove | read/add/remove/modify |
Implementation class | Anonymus class | Itr | ListItr |
Methods | hasMoreElements() nextElement() | hasNext() Next() Remove() | hasNext(); next(); remove(); previous(); hasPrevious(); add() set() previousIndex(); nextIndex(); |
Sorting Interfaces
Comparable
- Comparable interface is used for natural sorting order
- Comparable is used in TreeSet and TreeMap
Comparator
- Comparator interface is used for custom sorting order
- Comparator is used in TreeSet and TreeMap
Utility Classes
Arrays
- Arrays is utility class
- It has methods to perform some utility operations like sorting, searching, min, max, sum etc. on arrays
Collections
- Collections is utility class
- It has methods to perform some utility operations like sorting, searching, min, max, sum etc. on collection objects
What is the difference between Collection and Collections?
Collection is an interface which can be used for representing a group of individual objects as a single entity. But Collections is a utility class defined several utility(sorting, searching, sum, max, min …etc.) methods.