Collections Framework Overview
Reading Time: 10 Minutes
Difficulty: Beginner
Topic Summaryโ
The Java Collections Framework is a set of classes and interfaces that provide ready-made data structures for storing, organising, and manipulating groups of objects. Instead of writing your own linked list or hash table from scratch, Java gives you ArrayList, HashMap, HashSet, and many others โ all part of the java.util package.
What You'll Learnโ
- What the Collections Framework is and why it exists
- The hierarchy of collection interfaces (ASCII diagram)
- The difference between
Collection(interface) andCollections(utility class) - When to use
List,Set,Queue, andMap - Overview of the most important implementations
Prerequisitesโ
- Java Generics (type parameters like
<E>) - Java Interfaces and classes
- Basic Java syntax
Explanationโ
What Is the Collections Framework?โ
Before the Collections Framework (pre-Java 2), Java had Vector, Stack, Hashtable, and arrays โ inconsistent, poorly designed, and hard to use together. Java 2 introduced a unified framework with consistent interfaces and implementations.
The Collections Framework gives you:
- Data structures: Lists, Sets, Queues, Maps (Trees, Hash tables, Linked lists...)
- Algorithms: Sorting, searching, shuffling (via the
Collectionsutility class) - Interoperability: All collections work together through common interfaces
The Hierarchy โ ASCII Diagramโ
java.lang.Iterable
โโโ java.util.Collection (interface)
โโโ java.util.List (interface)
โ โโโ ArrayList
โ โโโ LinkedList
โ โโโ Vector (legacy)
โ โโโ Stack (legacy)
โ
โโโ java.util.Set (interface)
โ โโโ HashSet
โ โ โโโ LinkedHashSet
โ โโโ SortedSet (interface)
โ โโโ TreeSet
โ
โโโ java.util.Queue (interface)
โโโ LinkedList
โโโ PriorityQueue
โโโ Deque (interface)
โโโ ArrayDeque
โโโ LinkedList
java.util.Map (interface) โ NOT a Collection!
โโโ HashMap
โ โโโ LinkedHashMap
โโโ SortedMap (interface)
โ โโโ TreeMap
โโโ Hashtable (legacy)
โโโ Properties
Key observation: Map is NOT part of the Collection interface hierarchy โ it stands alone because it stores key-value pairs rather than individual elements.
Collection vs Collections โ Don't Confuse Them!โ
Collection | Collections | |
|---|---|---|
| Type | Interface | Class |
| Package | java.util | java.util |
| Purpose | Root interface for List, Set, Queue | Utility class with static methods |
| Examples | Collection<String> c = new ArrayList<>() | Collections.sort(list), Collections.shuffle(list) |
Collection (singular) defines what collections CAN DO.
Collections (plural) provides HELPER METHODS that work on collections.
List Interface โ Ordered, Allows Duplicatesโ
List is an ordered collection that preserves insertion order and allows duplicate elements. Each element has an index (position).
Use when: Order matters, duplicates are allowed, you need index-based access.
Key implementations:
ArrayListโ Fast random access by index, slow insertion/deletion in middleLinkedListโ Fast insertion/deletion anywhere, slow random access by indexVectorโ Legacy; like ArrayList but synchronized (useArrayListinstead)
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Alice"); // duplicates allowed!
System.out.println(names.get(0)); // Alice โ index access
Set Interface โ No Duplicates, No Guaranteed Orderโ
Set is a collection that does not allow duplicate elements. Most implementations do NOT preserve insertion order.
Use when: You need unique elements, you're checking membership, order doesn't matter.
Key implementations:
HashSetโ Fastest; no order guaranteed; uses hash tableLinkedHashSetโ Preserves insertion order; slightly slower than HashSetTreeSetโ Sorted in natural order; uses a Red-Black tree; slowest but sorted
Set<String> fruits = new HashSet<>();
fruits.add("apple");
fruits.add("banana");
fruits.add("apple"); // silently ignored โ already exists!
System.out.println(fruits.size()); // 2
Queue Interface โ FIFO (First In, First Out)โ
Queue is designed for holding elements prior to processing. Elements are added at the tail and removed from the head.
Use when: You need FIFO processing (tasks, print queues, breadth-first search).
Key implementations:
LinkedListโ General-purpose queuePriorityQueueโ Elements sorted by priority (smallest first by default)ArrayDequeโ Double-ended queue (deque); can add/remove from both ends
Queue<String> queue = new LinkedList<>();
queue.offer("First");
queue.offer("Second");
queue.offer("Third");
System.out.println(queue.poll()); // First (removed from head)
System.out.println(queue.peek()); // Second (looked at without removing)
Map Interface โ Key-Value Pairsโ
Map stores data as key-value pairs. Each key is unique; values can repeat. It's like a dictionary โ look up a value by its key.
Use when: You need to associate one thing with another (name โ phone number, word โ count).
Key implementations:
HashMapโ Fastest; no order on keys; allows one null keyLinkedHashMapโ Preserves insertion order of keysTreeMapโ Keys sorted in natural orderHashtableโ Legacy; synchronized; don't use (useConcurrentHashMapfor threads)
Map<String, Integer> phoneBook = new HashMap<>();
phoneBook.put("Alice", 12345);
phoneBook.put("Bob", 67890);
System.out.println(phoneBook.get("Alice")); // 12345
When to Use Which?โ
| Need | Use |
|---|---|
| Ordered list with duplicates | ArrayList |
| Frequent insert/delete in middle | LinkedList |
| Unique elements, fast lookup | HashSet |
| Unique elements, sorted | TreeSet |
| Unique elements, insertion order | LinkedHashSet |
| FIFO queue | LinkedList or ArrayDeque |
| Priority-based processing | PriorityQueue |
| Key-value pairs, fast lookup | HashMap |
| Key-value pairs, sorted by key | TreeMap |
| Key-value pairs, insertion order | LinkedHashMap |
Real-World Analogyโ
Think of the Collections Framework as different types of storage solutions at a supermarket:
List(ArrayList) = A numbered shelf โ each item has a position, duplicates OK, you can grab item #5 directlySet(HashSet) = A unique product catalogue โ no two identical entries; only one "Apple" listingQueue= A checkout line โ first customer in gets served first (FIFO)Map(HashMap) = A phone book โ look up any person's number by their name instantly
Code Exampleโ
import java.util.*;
public class CollectionsOverviewDemo {
public static void main(String[] args) {
// === LIST โ ordered, duplicates allowed ===
List<String> shoppingList = new ArrayList<>();
shoppingList.add("Milk");
shoppingList.add("Bread");
shoppingList.add("Milk"); // Duplicate allowed
System.out.println("List: " + shoppingList); // [Milk, Bread, Milk]
System.out.println("Item at index 1: " + shoppingList.get(1)); // Bread
// === SET โ no duplicates ===
Set<String> uniqueFruits = new HashSet<>();
uniqueFruits.add("Apple");
uniqueFruits.add("Banana");
uniqueFruits.add("Apple"); // Ignored โ already exists
System.out.println("Set: " + uniqueFruits); // [Apple, Banana] (order may vary)
System.out.println("Set size: " + uniqueFruits.size()); // 2
// === QUEUE โ FIFO ===
Queue<String> printJobs = new LinkedList<>();
printJobs.offer("Document1.pdf");
printJobs.offer("Photo.jpg");
printJobs.offer("Report.docx");
System.out.println("Next print job: " + printJobs.poll()); // Document1.pdf
System.out.println("Remaining queue: " + printJobs);
// === MAP โ key-value pairs ===
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 88);
scores.put("Charlie", 92);
System.out.println("Alice's score: " + scores.get("Alice")); // 95
System.out.println("Map: " + scores);
// === Collections utility class ===
List<Integer> numbers = new ArrayList<>(Arrays.asList(5, 2, 8, 1, 9, 3));
System.out.println("Before sort: " + numbers);
Collections.sort(numbers);
System.out.println("After sort: " + numbers);
System.out.println("Max: " + Collections.max(numbers));
System.out.println("Min: " + Collections.min(numbers));
}
}
Outputโ
List: [Milk, Bread, Milk]
Item at index 1: Bread
Set: [Apple, Banana]
Set size: 2
Next print job: Document1.pdf
Remaining queue: [Photo.jpg, Report.docx]
Alice's score: 95
Map: {Alice=95, Bob=88, Charlie=92}
Before sort: [5, 2, 8, 1, 9, 3]
After sort: [1, 2, 3, 5, 8, 9]
Max: 9
Min: 1
Common Mistakesโ
- โ Mistake: Confusing
Collection(interface) withCollections(utility class) โ โ Fix: Remember โ one is a type, the other has static methods likesort()andshuffle() - โ Mistake: Using
Mapas if it extendsCollectionโ โ Fix:Mapis a separate hierarchy โ it doesn't implementCollection - โ Mistake: Choosing the wrong data structure (e.g.,
ArrayListwhen uniqueness matters) โ โ Fix: Think about requirements first โ ordered? unique? key-value? frequency? - โ Mistake: Using legacy classes (
Vector,Hashtable) โ โ Fix: UseArrayList,HashMap, orConcurrentHashMapfor threads
Best Practicesโ
- Program to the interface: use
List<String> list = new ArrayList<>()notArrayList<String> list = new ArrayList<>() - Choose the data structure based on your needs before writing code
- Use
Collections.unmodifiableList()to create read-only views of collections - Set initial capacity when you know approximate size:
new ArrayList<>(1000)avoids resizing - Prefer
isEmpty()oversize() == 0for checking empty collections
Interview Questionsโ
Q: What is the difference between Collection and Collections in Java?
A: Collection (singular) is a root interface in the Java Collections Framework that is extended by List, Set, and Queue. Collections (plural) is a utility class in java.util that provides static methods for operating on collections, like sort(), shuffle(), max(), min(), frequency(), and unmodifiableList().
Q: Is Map a part of the Collection interface?
A: No. Map is NOT part of the Collection interface hierarchy. While List, Set, and Queue all extend Collection, Map is completely separate because it stores key-value pairs rather than individual elements. However, Map is still part of the Java Collections Framework.
Q: What are the key differences between List, Set, and Map?
A: List is ordered, allows duplicates, supports index-based access. Set does not allow duplicates, most implementations have no guaranteed order. Map stores key-value pairs where each key is unique โ it's not a collection of individual elements but of entries.
Quick Revisionโ
โ Collections Framework = unified library of data structures in java.util
โ Collection is an interface; Collections is a utility class with static methods
โ Map is NOT a Collection โ separate hierarchy
โ List = ordered + duplicates; Set = unique; Queue = FIFO; Map = key-value
โ Program to interfaces: List<T>, not ArrayList<T> on the left side
Related Topicsโ
- ArrayList (Lesson 02)
- HashMap (Lesson 04)
- Generics โ used everywhere in collections
Next Lessonโ
02 - ArrayList