What is the Collection Framework?
• The Java Collection Framework (JCF) is a set of classes and interfaces that implement
commonly used data structures (like lists, sets, queues, maps).
• Provides ready-made implementations (ArrayList, HashSet, HashMap, etc.) instead of
writing them from scratch.
• All are present in the package:
Collection Interface
• Collection is the root interface of the Java Collection Framework.
• It defines common methods that all collections (List, Set, Queue) should have.
Common Methods in Collection:
Method Description
add(E e) Add element
remove(Object o) Remove element
size() Number of elements
contains(Object o) Check if element exists
isEmpty() Check if collection is empty
clear() Remove all elements
iterator() Get iterator for looping
Collection Framework Hierarchy
Core Interfaces in Collection Framework
Allows
Interface Description Order Maintained?
Duplicates?
Ordered collection,
List Yes Yes
index-based access
Depends (HashSet = no order,
Unique elements
Set No LinkedHashSet = insertion order, TreeSet
only
= sorted order)
FIFO (first-in, first-
Queue Yes Yes (insertion order)
out)
Key-value pairs, keys No (keys),
Map Depends on implementation
unique Yes (values)
Key Comparisons
• ArrayList vs LinkedList
o ArrayList → fast access (index-based)
o LinkedList → fast insertion/deletion
• HashSet vs TreeSet vs LinkedHashSet
o HashSet → no order
o LinkedHashSet → maintains insertion order
o TreeSet → maintains sorted order
• HashMap vs TreeMap vs LinkedHashMap
o HashMap → no order
o LinkedHashMap → maintains insertion order
o TreeMap → sorted by keys
Summary:
• Collection Framework = Interfaces (List, Set, Queue, Map) + Classes (ArrayList,
HashSet, HashMap, etc.)
• It provides ready-made, efficient data structures with many utility methods.
Java Collections Comparison Table
Quick Use-Cases
• ArrayList → when you need fast random access (e.g., store student list, read often,
modify rarely).
• LinkedList → when you do many insertions/deletions (e.g., playlist management).
• HashSet → when you want unique elements, no duplicates (e.g., store employee
IDs).
• LinkedHashSet → when you want unique + preserve order (e.g., ordered unique
items in cart).
• TreeSet → when you need sorted unique elements (e.g., leaderboard, ranking
system).
• PriorityQueue → when you want elements processed by priority (e.g., CPU
scheduling).
• ArrayDeque → when you want a double-ended queue (e.g., undo/redo operations).
• HashMap → when you want fast key-value lookup (e.g., dictionary, cache).
• LinkedHashMap → when you want key-value with order preserved (e.g., LRU
cache).
• TreeMap → when you need sorted key-value pairs (e.g., phonebook sorted by
name).
Interfaces extending Collection
Collection has three main sub-interfaces:
Collection
├─ List // Ordered, duplicates allowed
├─ Set // Unordered, no duplicates
└─ Queue // FIFO, allows insertion/removal from ends
• List, Set, Queue are interfaces, and they inherit from Collection.
• This means all methods in Collection are available in List, Set, Queue.
• Each interface also adds its specific behavior.
Map is separate
• Map does NOT extend Collection, because a map stores key-value pairs, not individual
elements.
• That’s why Map has its own interface and methods:
Map Method Description
put(K key, V value) Add key-value pair
get(K key) Get value by key
remove(K key) Remove key-value pair
containsKey(K key) Check if key exists
containsValue(V value) Check if value exists
keySet() Get all keys
values() Get all values
entrySet() Get key-value pairs