Java Collections
Resizable, feature-rich containers — far more powerful than arrays.
The hierarchy
The Java Collections Framework provides interfaces and implementations for storing groups of objects:
- List — ordered, allows duplicates, indexable.
ArrayList,LinkedList. - Set — no duplicates, unordered (or ordered, depending on impl).
HashSet,TreeSet,LinkedHashSet. - Map — key/value pairs, unique keys.
HashMap,TreeMap,LinkedHashMap. (Map isn't strictly a Collection but lives in the same framework.) - Queue / Deque — FIFO/LIFO access.
ArrayDeque,LinkedList.
ArrayList — the dynamic array
Most-used list implementation. Backed by an array, fast random access, slower inserts in the middle.
ArrayList basics
import java.util.ArrayList; import java.util.List; List<String> names = new ArrayList<>(); names.add("Raman"); names.add("Aman"); names.add("Priya"); System.out.println(names.size()); // 3 System.out.println(names.get(0)); // "Raman" names.remove(1); // removes "Aman" names.set(0, "Sara"); // replace at index 0 System.out.println(names.contains("Priya")); // true for (String n : names) { System.out.println(n); }
HashSet — uniqueness, fast lookup
HashSet
import java.util.HashSet; import java.util.Set; Set<String> cities = new HashSet<>(); cities.add("Bangalore"); cities.add("Mumbai"); cities.add("Bangalore"); // duplicate ignored System.out.println(cities.size()); // 2 System.out.println(cities.contains("Mumbai")); // true (O(1) lookup)
HashMap — key/value lookup
HashMap
import java.util.HashMap; import java.util.Map; Map<String, Integer> ages = new HashMap<>(); ages.put("Raman", 28); ages.put("Aman", 22); ages.put("Priya", 25); System.out.println(ages.get("Raman")); // 28 System.out.println(ages.containsKey("Aman")); // true ages.remove("Aman"); // Iterate for (Map.Entry<String, Integer> e : ages.entrySet()) { System.out.println(e.getKey() + " → " + e.getValue()); }
Choosing the right collection
| Need | Use |
|---|---|
| Ordered list of items, frequent access by index | ArrayList |
| Frequent insertions/removals at both ends | ArrayDeque or LinkedList |
| Set of unique items, fast contains check | HashSet |
| Sorted unique items | TreeSet |
| Lookup by key | HashMap |
| Lookup by key, sorted iteration | TreeMap |
| Lookup by key, preserves insertion order | LinkedHashMap |
Generics — type safety
Notice the <String> in ArrayList<String>. That's a generic type parameter telling the compiler what kind of element this list holds. Without it, you could put anything in (and pay later with ClassCastException at runtime).
Common operations with streams (Java 8+)
Stream pipeline
List<Integer> numbers = List.of(1, 2, 3, 4, 5); int sumOfEvenSquares = numbers.stream() .filter(n -> n % 2 == 0) .mapToInt(n -> n * n) .sum(); System.out.println(sumOfEvenSquares); // 4 + 16 = 20
Program to interfaces
Declare variables as
List<String> not ArrayList<String>. That way you can swap implementations later (e.g., to LinkedList or List.of(...)) without changing every line that uses the variable.