11.1 Collections Framework Overview
Note: This guide is written based on Java 21.
The Array is the most fundamental tool for gathering multiple pieces of data in one place. However, arrays harbor a critical design limitation: their size must be fixed from the very start. If you create an array to store 10 student names and an 11th student enrolls later? You'd have to create an entirely new, larger array and copy everything over — an enormous inconvenience.
To solve this, Java provides standardized dynamic data structures whose sizes grow and shrink freely as part of its standard library. This is collectively known as the Collections Framework.
1. What is the Collections Framework?
The Collections Framework is a massive library system that standardizes common operations for storing, querying, modifying, and deleting data through a set of interfaces and classes.
It lives inside the java.util package and is truly one of the most heavily used toolsets in everyday Java development.
2. Three Core Interfaces
At the very top of the Collections Framework hierarchy sit three essential interfaces.
| Interface | Characteristics | Key Implementation Classes |
|---|---|---|
List | Ordered, ** allows duplicates** | ArrayList, LinkedList |
Set | Unordered, ** no duplicates**(auto-removed) | HashSet, TreeSet |
Map | Stores ** Key-Value**pairs, keys must be unique | HashMap, TreeMap |
3. When to Use Which?
- Need to store data sequentially, like a numbered list?→
List(ArrayList) - Need a collection of only unique values with zero duplicates?→
Set(HashSet) - Need to look up a value instantly using a name (Key), like a dictionary?→
Map(HashMap)
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashMap;
// List: Allows duplicates, preserves insertion order
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Apple"); // Duplicate is stored as-is
System.out.println(list); // [Apple, Banana, Apple]
// Set: Automatically removes duplicates, no guaranteed order
HashSet<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // Duplicate is silently ignored
System.out.println(set); // [Banana, Apple] (order not guaranteed)
// Map: Stores key-value pairs
HashMap<String, Integer> map = new HashMap<>();
map.put("Math", 95);
map.put("Science", 88);
map.put("English", 92);
System.out.println(map.get("Science")); // 88
In the upcoming chapters, we will study each interface's primary implementation classes (ArrayList, HashSet, HashMap) one by one in full detail.