Generics and Lambda Expressions

Lesson 19 of 20

Write code once for every type: angle-bracket generics checked at compile time, lambdas that shrink boilerplate to one line.

The big picture

Angle brackets and arrows

Generics let one class work for every type — safely, at compile time. Lambdas let you pass behavior itself, with no ceremony.

T is a placeholder

List<String> fills the T slot with String; the compiler then rejects every wrong-type attempt.

No more casts

get(0) on a List<String> returns a String — no (String) cast, no ClassCastException risk.

Lambda = tiny function

n -> System.out.println(n) is a method compressed to its input and its body.

Where lambdas shine

forEach, sort and filters accept them — behavior travels like data.

2004

Java 5

Generics arrive: collections finally know what they hold.

2014

Java 8

Lambda expressions and Streams change how behavior is passed around.

today

Everywhere

List<T>, Map<K,V> and lambdas are the everyday Java style.