Java Collections Framework
The Java Collections Framework is a unified architecture for storing, retrieving, and manipulating groups of objects. Unlike arrays, collections are dynamic — they can grow and shrink automatically. The framework provides interfaces, implementations, and algorithms to handle common data structures.
All collection classes are in the java.util package.
Collection Hierarchy Overview
Collection (interface)
├── List (interface) – Ordered, allows duplicates
│ ├── ArrayList
│ ├── LinkedList
│ └── Vector
├── Set (interface) – Unordered, no duplicates
│ ├── HashSet
│ ├── LinkedHashSet
│ └── TreeSet
└── Queue (interface) – FIFO ordering
├── PriorityQueue
└── LinkedList
Map (interface – separate from Collection)
├── HashMap
├── LinkedHashMap
└── TreeMap1. ArrayList
ArrayList is the most widely used collection. It is a resizable array that maintains insertion order and allows duplicate elements. It provides fast access by index.
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
// Adding elements
fruits.add("Mango");
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple"); // duplicates allowed
System.out.println("List: " + fruits);
System.out.println("Size: " + fruits.size());
System.out.println("Element at index 1: " + fruits.get(1));
// Removing an element
fruits.remove("Banana");
System.out.println("After removal: " + fruits);
// Iterating
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}Output:
List: [Mango, Apple, Banana, Apple]
Size: 4
Element at index 1: Apple
After removal: [Mango, Apple, Apple]
Mango
Apple
Apple2. LinkedList
LinkedList stores elements as a doubly linked list. It is better than ArrayList for frequent insertions and deletions at the beginning or middle. It also implements the Deque interface, so it can act as a stack or queue.
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<String> tasks = new LinkedList<>();
tasks.add("Write code");
tasks.addFirst("Plan project"); // adds to front
tasks.addLast("Test code"); // adds to end
System.out.println(tasks); // [Plan project, Write code, Test code]
System.out.println("First: " + tasks.getFirst());
System.out.println("Last: " + tasks.getLast());
tasks.removeFirst();
System.out.println("After removeFirst: " + tasks);
}
}3. HashSet
HashSet stores unique elements with no guaranteed order. It is backed by a hash table and offers very fast lookup, add, and remove operations.
import java.util.HashSet;
public class HashSetDemo {
public static void main(String[] args) {
HashSet<String> cities = new HashSet<>();
cities.add("Mumbai");
cities.add("Delhi");
cities.add("Chennai");
cities.add("Delhi"); // duplicate – will not be added
System.out.println("Cities: " + cities);
System.out.println("Contains Chennai: " + cities.contains("Chennai"));
cities.remove("Chennai");
System.out.println("After removal: " + cities);
}
}4. TreeSet
TreeSet stores elements in sorted ascending order with no duplicates. It is implemented using a Red-Black tree internally.
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Integer> scores = new TreeSet<>();
scores.add(85);
scores.add(72);
scores.add(95);
scores.add(60);
scores.add(85); // duplicate – ignored
System.out.println("Sorted scores: " + scores); // [60, 72, 85, 95]
System.out.println("Highest: " + scores.last());
System.out.println("Lowest: " + scores.first());
}
}5. HashMap
HashMap stores data as key-value pairs. Each key is unique; values can be duplicated. It offers fast retrieval based on keys. The order of elements is not guaranteed.
import java.util.HashMap;
public class HashMapDemo {
public static void main(String[] args) {
HashMap<String, Integer> scores = new HashMap<>();
scores.put("Alice", 92);
scores.put("Bob", 78);
scores.put("Carol", 88);
System.out.println("All scores: " + scores);
System.out.println("Alice's score: " + scores.get("Alice"));
scores.put("Bob", 85); // updates existing key
System.out.println("Updated Bob: " + scores.get("Bob"));
scores.remove("Carol");
System.out.println("After removal: " + scores);
// Iterating
for (String name : scores.keySet()) {
System.out.println(name + " → " + scores.get(name));
}
}
}Output:
All scores: {Alice=92, Bob=78, Carol=88}
Alice's score: 92
Updated Bob: 85
After removal: {Alice=92, Bob=85}
Alice → 92
Bob → 856. LinkedHashMap
LinkedHashMap works like HashMap but maintains insertion order. Useful when the order of entries matters.
import java.util.LinkedHashMap;
LinkedHashMap<String, Integer> menu = new LinkedHashMap<>();
menu.put("Coffee", 50);
menu.put("Tea", 30);
menu.put("Juice", 70);
for (var entry : menu.entrySet()) {
System.out.println(entry.getKey() + ": Rs." + entry.getValue());
}Output (insertion order preserved):
Coffee: Rs.50
Tea: Rs.30
Juice: Rs.707. PriorityQueue
PriorityQueue processes elements in natural order (smallest first by default) rather than insertion order. Useful for task scheduling based on priority.
import java.util.PriorityQueue;
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(30);
pq.add(10);
pq.add(20);
while (!pq.isEmpty()) {
System.out.print(pq.poll() + " "); // removes and returns smallest
}Output:
10 20 30 Collections Utility Class
The Collections class provides static utility methods for working with collections.
import java.util.ArrayList;
import java.util.Collections;
ArrayList<Integer> list = new ArrayList<>();
list.add(5); list.add(2); list.add(8); list.add(1);
Collections.sort(list);
System.out.println("Sorted: " + list);
Collections.reverse(list);
System.out.println("Reversed: " + list);
System.out.println("Max: " + Collections.max(list));
System.out.println("Min: " + Collections.min(list));Comparison of Common Collections
| Class | Ordered? | Duplicates? | Key-Value? | Sorted? |
|---|---|---|---|---|
| ArrayList | Yes (insertion) | Yes | No | No |
| LinkedList | Yes (insertion) | Yes | No | No |
| HashSet | No | No | No | No |
| TreeSet | Yes (sorted) | No | No | Yes |
| HashMap | No | Values yes | Yes | No |
| LinkedHashMap | Yes (insertion) | Values yes | Yes | No |
| TreeMap | Yes (sorted by key) | Values yes | Yes | Yes |
Summary
- The Collections Framework provides ready-to-use data structures for storing and managing groups of objects.
ArrayListis ordered, resizable, and allows duplicates.HashSetis unordered and allows no duplicates.TreeSetkeeps elements sorted with no duplicates.HashMapstores key-value pairs with fast lookup.- The
Collectionsutility class offers sorting, searching, and other helpful operations.
