Collections, Generics and Functional Java
Harry
· 16 Sep 2026
· 17 views
Log in to track your progress and mark lessons complete.
Sponsored
The Collections Framework
- List (
ArrayList) – ordered, allows duplicates, indexed. - Set (
HashSet) – no duplicates, no order guarantee. - Map (
HashMap) – key → value pairs, unique keys.
List<String> names = new ArrayList<>();
names.add("Ada");
Map<String,Integer> scores = new HashMap<>();
scores.put("Ada", 95);
Generics
Generics give compile-time type safety: List<String> can only hold Strings, so no casting and no ClassCastException at runtime. Expect exam questions on bounded types (<? extends Number>).
Lambdas and streams
Functional Java is heavily tested. A lambda implements a functional interface; the Stream API processes collections declaratively:
List<Integer> nums = List.of(1, 2, 3, 4);
int sumEven = nums.stream()
.filter(n -> n % 2 == 0)
.mapToInt(Integer::intValue)
.sum(); // 6
Key points
- Choose List, Set or Map by ordering, uniqueness and lookup needs.
- Generics give compile-time type safety and remove casts.
- Lambdas implement functional interfaces (like
Predicate). - Streams (
filter/map/reduce) are core exam material.