Overview of Collections
The Java Collections Framework provides data structures and algorithms for storing and manipulating groups of objects.
- List — ordered, allows duplicates (ArrayList, LinkedList)
- Set — no duplicates (HashSet, TreeSet)
- Map — key-value pairs (HashMap, TreeMap)
- Queue — FIFO ordering (LinkedList, PriorityQueue)
- Stack — LIFO ordering (Stack, Deque)
Using Collections
Collections use generics to ensure type safety.
Example
import java.util.*;
// List
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
names.get(0); // "Alice"
names.size(); // 3
names.contains("Bob"); // true
names.remove("Bob");
// Set
Set<Integer> numbers = new HashSet<>();
numbers.add(1);
numbers.add(2);
numbers.add(1); // ignored — no duplicates
// Map
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 88);
scores.get("Alice"); // 95
scores.containsKey("Bob"); // true 