Module 13: Functional Programming and Streams
Lambda Syntax Made Simple
Learn lambda syntax in a simple way so method-based interfaces feel less verbose.
Author
Java Learner Editorial Team
Reviewer
Technical review by Java Learner
Last reviewed
2026-04-17
Java version
Java 25 LTS
Learning goals
- Recognize lambda-friendly code
- Read and write simple lambda expressions
- Understand that lambdas represent behavior, not magic
A lambda is a compact way to pass behavior: Instead of writing a full anonymous class, you can pass a short block or expression.
This is useful when code wants “what to do” as a value: Sorting, filtering, event handling, and stream operations rely on this heavily.
Keep lambdas small: If the logic becomes large, move it into a named method for clarity.
Think in plain English: “When each item arrives, do this.” That mindset makes lambdas easier to read.
Runnable examples
Sort names by length with a lambda
import java.util.ArrayList;
import java.util.List;
List<String> names = new ArrayList<>(List.of("Ada", "Grace", "Linus"));
names.sort((a, b) -> Integer.compare(a.length(), b.length()));
System.out.println(names);Expected output
[Ada, Grace, Linus]
Mini exercise
Sort a list of city names alphabetically using a lambda comparator.
Summary
- Lambdas let you pass behavior more compactly.
- They work best when the logic is short and focused.
- They become especially useful in stream pipelines.
Next step
To use lambdas well, you need the functional interfaces they target.
Sources used