Collections, Generics and Functional Java

Harry · 16 Sep 2026 · 17 views
Log in to track your progress and mark lessons complete.

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.
Share this post:

Comments (0)

Please login or register to comment.

Create a free account to keep reading

You've enjoyed a free tutorial! Register (it's free) to unlock every tutorial, track your progress and save code.

Already have an account? Log in