Guided Notes

Generics, Lambdas & Streams

Generic classes and methods, lambda expressions, method references, Comparators, and stream operations.

Generics

Slide 1 / 5

Generics let a class or method work with any type while staying type-safe:

class Box<T> { private T value; Box(T value) { this.value = value; } T get() { return value; } } Box<String> b = new Box<>("hello"); String s = b.get(); // no cast needed

Generic methods declare their type parameter before the return type:

static <T> T firstOf(List<T> list) { return list.get(0); }

Bounded types restrict T: <T extends Comparable<T>> allows only comparable types.

← Collections