Complete Guide to Java Collections Framework (JCF) - From Basics to Advanced

The Java Collections Framework (JCF) is one of the most powerful parts of Java. It provides efficient data structures and algorithms to store, process, and manipulate groups of objects.
Understanding collections deeply is essential for:
Writing efficient programs
Cracking technical interviews
Building scalable applications
In this article, we will explore the entire Java Collections Framework step by step, including:
Collection Hierarchy
List implementations
Set implementations
Queue and Deque
Map interface
Iterators
Comparable vs Comparator
Utility classes
Best practices
1. What is the Java Collections Framework?
The Java Collections Framework(JCF) is a set of classes and interfaces that provides architecture to store and manipulate groups of objects dynamically.
It includes:
Interfaces
Implementations
Algorithms
It is part of the java.util package.
Key Benefits:
Reduces programming effort
Improves performance
Provides reusable data structures
Increases code readability
2. Collection Framework Hierarchy
The main root interface are:
Iterable
|
Collection
|
---------------------------------------
| | |
List Set Queue
The Map interface is sepearate from the Collection hierarchy.
Map
|
--------------------------------------------
| | |
HashMap LinkedHashMap TreeMap
3. Collection Interface
The Collection interface is the root interface of the Collection hierarchy.
It represents a group of objects called elements.
Important Methods
add()
remove()
size()
isEmpty()
contains()
clear()
iterator()
Example:
import java.util.*;
public class Test{
public static void main(String[] args){
Collection<String> c = new ArrayList<>();
c.add("Java");
c.add("Python");
c.add("C++");
System.out.println(c);
}
}
Output:
[Java, Python, C++]
4. List Interface
The List interface represents an ordered collection.
Features
Maintains insertion order
Allows duplicate elements
Supports index-based access
Common List Implementations
ArrayList
LinkedList
Vector
Stack
5. ArrayList
ArrayList is the most widely used implementation of the List interface.
It is based on a dynamic array.
Features
Allows duplicates
Maintains insertion order
Random access is fast
Not synchronized
Example
import java.util.*;
public class Example {
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
System.out.println(list);
}
}
Output:
[10, 20, 30]
Internal Working:
Default capacity = 10
When full, capacity increases by (oldCapacity * 1.5)
6. LinkedList
LinkedList is based on a doubly linked list.
Features
Fast insertion and deletion
Slower random access
Maintains order
Allows duplicates
Example
LinkedList<String> list = new LinkedList<>();
list.add("Java");
list.add("Spring");
list.add("React");
System.out.println(list);
When to use LinkedList?
Use LinkedList when:
Frequent insertion
Frequent deletion
Sequential access
7. Vector
Vector is similar to ArrayList but it is synchronized.
Features
Thread safe
Legacy class
Slower than ArrayList
Example
Vector<Integer> v = new Vector<>();
v.add(10);
v.add(20);
8. Stack
Stack follows LIFO(Last In First Out)
Operations
push()
pop()
peek()
Example
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
System.out.println(stack.pop());
9. Set Interface
The Set interface represents a collection that does not allow duplicate elements.
Characteristics
No duplicates
No indexing
Used for unique elements
Set Implementations
HashSet
LinkedHashSet
TreeSet
10. HashSet
HashSet uses a hash table for storing elements.
Features
No duplicates
no order guarantee
Allows one null value
Fast Performance
Example
HashSet<String> set = new HashSet<>();
set.add("Java");
set.add("Python");
set.add("Java");
System.out.println(set);
Output
[Java, Python]
Duplicate is automatically removed.
11. LinkedHashSet
LinkedHashSet is similar to HashSet but maintains insertion order.
Example
LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("Java");
set.add("Python");
set.add("C++");
System.out.println(set);
Output
[Java, Python, C++]
12. TreeSet
TreeSet stores elements in sorted order. It uses a Red-Black Tree internally.
Features
Sorted order
No duplicates
Does not allow null
Example
TreeSet<Integer> set = new TreeSet<>();
set.add(30);
set.add(10);
set.add(20);
System.out.println(set);
Output
[10, 20, 30]
13. Queue Interface
Queue follows FIFO(First In First Out) principle.
Common Implementations:
PriorityQueue
ArrayDeque
LinkedList
14. PriorityQueue
Elements are processed based on priority order.
Example
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(30);
pq.add(10);
pq.add(20);
System.out.println(pq);
Output
[10,20,30]
15. Map Interface
Map stores Key-value pairs.
Characteristics
Unique keys
Duplicate values allowed
Not part of Collection interface
Map implementations
HashMap
LinkedHashMap
TreeMap
Hashtable
16. HashMap
Most commonly used Map implementation.
Features
Fast lookup
Allows one null key
Allows multiple null values
Not synchronized
Example
HashMap<Integer,String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Python");
map.put(3, "C++");
System.out.println(map);
17. LinkedHashMap
Maintains insertion order.
Example
LinkedHashMap<Integer,String> map = new LinkedHashMap<>();
18. TreeMap
Stores elements in sorted order of keys.
Example
TreeMap<Integer,String> map = new TreeMap<>();
19. Iterator
Iterator is used to traverse collection elements.
Methods
hasNext()
next()
remove()
Example
Iterator<Integer> it = list.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
20. Comparable vs Comparator
These are used for custom sorting.
Comparable:
Defined inside the class.
compareTo()
Example
class Student implements Comparable<Student> {
int id;
public int compareTo(Student s) {
return this.id - s.id;
}
}
Comparator:
Defined outside the class.
compare()
Example
class SortByName implements Comparator<Student> {
public int compare(Student a, Student b){
return a.name.compareTo(b.name);
}
}
21. Collections Utility Class
The Collections class provides useful utility methods.
Examples
Collections.sort()
Collections.reverse()
Collections.shuffle()
Collections.max()
Collections.min()
22. When to Use Which Collection?
ArrayList ------> Fast access
LinkedList --------> Frequent insert / delete
HashSet ---------> Unique elements
TreeSet --------> Sorted unique elements
HashMap --------> Fast key-value lookup
TreeMap ---------> Sorted keys
23. Best Practices
Prefer interfaces over implementations.
Use ArrayList for most cases.
Use HashMap for fast retrieval.
Avoid Vector and Hashtable in modern applications.
Conclusion
The Java Collections Framework provides powerful data structures to efficiently manage groups of objects.
Mastering collections helps developers:
Write optimized code
Solve complex problems
Crack coding interviews
Build scalable applications






