π Introduction
When building real-world applications, we often need to store, retrieve, and manipulate groups of objects. Java provides a rich and powerful Collections Framework to make this easy.
In this lesson, weβll explore:
List
(ArrayList, LinkedList)Set
(HashSet, TreeSet)Map
(HashMap, TreeMap)Queue
(PriorityQueue, LinkedList)
π§± What is the Java Collections Framework?
A Collection is a container object that holds a group of elements.
The Collections Framework provides:
- Interfaces (
List
,Set
,Map
,Queue
) - Implementations (
ArrayList
,HashMap
, etc.) - Utility classes (
Collections
,Arrays
)
π List β Ordered Collection (Duplicates Allowed)
πΉ ArrayList
import java.util.ArrayList;
public class ListExample {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
β
Preserves insertion order
β
Allows duplicates
β
Dynamic resizing
πΉ LinkedList
Similar to ArrayList
, but optimized for frequent insertions/deletions.
LinkedList<String> items = new LinkedList<>();
items.add("One");
items.addFirst("Zero");
items.addLast("Two");
π« Set β No Duplicates Allowed
πΉ HashSet
import java.util.HashSet;
public class SetExample {
public static void main(String[] args) {
HashSet<String> cities = new HashSet<>();
cities.add("Delhi");
cities.add("Mumbai");
cities.add("Delhi");
System.out.println(cities); // Duplicates ignored
}
}
β
No duplicates
β Unordered (no guarantee of insertion order)
πΉ TreeSet β Sorted Set
import java.util.TreeSet;
TreeSet<Integer> numbers = new TreeSet<>();
numbers.add(10);
numbers.add(5);
numbers.add(15);
System.out.println(numbers); // Output: [5, 10, 15]
πΊοΈ Map β Key-Value Pairs
πΉ HashMap
import java.util.HashMap;
public class MapExample {
public static void main(String[] args) {
HashMap<String, Integer> ages = new HashMap<>();
ages.put("Alice", 25);
ages.put("Bob", 30);
System.out.println(ages.get("Alice")); // Output: 25
}
}
β
Fast lookup
β
Keys must be unique
β
Values can be duplicated
πΉ TreeMap β Sorted by Keys
import java.util.TreeMap;
TreeMap<String, Integer> scores = new TreeMap<>();
scores.put("Math", 90);
scores.put("English", 85);
System.out.println(scores); // Sorted by keys
π€οΈ Queue β FIFO (First In First Out)
πΉ PriorityQueue
import java.util.PriorityQueue;
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(20);
pq.add(10);
pq.add(30);
while (!pq.isEmpty()) {
System.out.println(pq.poll()); // Sorted order: 10, 20, 30
}
β Prioritizes elements (natural order by default)
π§ Utility Class: Collections
import java.util.Collections;
ArrayList<String> names = new ArrayList<>();
names.add("John");
names.add("Alice");
Collections.sort(names);
System.out.println(names); // [Alice, John]
π§ Quick Summary
Interface | Allows Duplicates | Maintains Order | Sorted | Key-Value |
---|---|---|---|---|
List | β Yes | β Yes | β No | β No |
Set | β No | β No (HashSet) | β (TreeSet) | β No |
Map | β Keys | β No | β (TreeMap) | β Yes |
Queue | β Yes | β FIFO / Priority | β (PriorityQueue) | β No |
π§ͺ Mini Challenge
- Create a
HashMap<String, Integer>
to store the names and scores of 3 students. - Use a
TreeSet<Integer>
to keep their scores sorted.
β What You Learned
- Difference between List, Set, Map, and Queue
- When to use ArrayList, HashSet, HashMap, etc.
- How to choose the right data structure based on your needs