Skip to main content

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) and Collections (utility class)
  • When to use List, Set, Queue, and Map
  • 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 Collections utility 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!โ€‹

CollectionCollections
TypeInterfaceClass
Packagejava.utiljava.util
PurposeRoot interface for List, Set, QueueUtility class with static methods
ExamplesCollection<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 middle
  • LinkedList โ€” Fast insertion/deletion anywhere, slow random access by index
  • Vector โ€” Legacy; like ArrayList but synchronized (use ArrayList instead)
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 table
  • LinkedHashSet โ€” Preserves insertion order; slightly slower than HashSet
  • TreeSet โ€” 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 queue
  • PriorityQueue โ€” 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 key
  • LinkedHashMap โ€” Preserves insertion order of keys
  • TreeMap โ€” Keys sorted in natural order
  • Hashtable โ€” Legacy; synchronized; don't use (use ConcurrentHashMap for 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?โ€‹

NeedUse
Ordered list with duplicatesArrayList
Frequent insert/delete in middleLinkedList
Unique elements, fast lookupHashSet
Unique elements, sortedTreeSet
Unique elements, insertion orderLinkedHashSet
FIFO queueLinkedList or ArrayDeque
Priority-based processingPriorityQueue
Key-value pairs, fast lookupHashMap
Key-value pairs, sorted by keyTreeMap
Key-value pairs, insertion orderLinkedHashMap

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 directly
  • Set (HashSet) = A unique product catalogue โ€” no two identical entries; only one "Apple" listing
  • Queue = 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) with Collections (utility class) โ†’ โœ… Fix: Remember โ€” one is a type, the other has static methods like sort() and shuffle()
  • โŒ Mistake: Using Map as if it extends Collection โ†’ โœ… Fix: Map is a separate hierarchy โ€” it doesn't implement Collection
  • โŒ Mistake: Choosing the wrong data structure (e.g., ArrayList when uniqueness matters) โ†’ โœ… Fix: Think about requirements first โ€” ordered? unique? key-value? frequency?
  • โŒ Mistake: Using legacy classes (Vector, Hashtable) โ†’ โœ… Fix: Use ArrayList, HashMap, or ConcurrentHashMap for threads

Best Practicesโ€‹

  • Program to the interface: use List<String> list = new ArrayList<>() not ArrayList<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() over size() == 0 for 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


  • ArrayList (Lesson 02)
  • HashMap (Lesson 04)
  • Generics โ€” used everywhere in collections

Next Lessonโ€‹

02 - ArrayList