Lambda Expressions in Java
In this lesson: Write concise code with Java lambda expressions — syntax, functional interfaces, forEach and sorting — with output.
What is a lambda
A lambda expression is a short, unnamed function you can pass around as a value. Introduced in Java 8, it uses the arrow -> and lets you write behaviour compactly — perfect for sorting, looping and event handling without writing full classes.
(parameters) -> body.The lambda syntax
// (parameters) -> body
(x) -> x * 2 // one parameter
(a, b) -> a + b // two parameters
() -> System.out.println("Hi") // no parameters
x -> { // block body
int y = x + 1;
return y;
}
The left side lists parameters, the arrow separates, and the right side is the body. Single expressions return automatically; a { } block uses return.
Functional interfaces
interface Greeter {
void greet(String name); // one abstract method
}
// lambda provides that method:
Greeter g = name -> System.out.println("Hi " + name);
g.greet("Aman");
A lambda works with a functional interface — an interface with exactly one abstract method. The lambda becomes that method's implementation, with no class needed.
Using lambdas with lists
import java.util.ArrayList; ArrayList<Integer> nums = new ArrayList<>(); nums.add(3); nums.add(1); nums.add(2); nums.forEach(n -> System.out.println(n)); // print each nums.sort((a, b) -> a - b); // sort ascending System.out.println(nums);
1
2
[1, 2, 3]
Lambdas shine with collections: forEach takes a lambda to run on each item, and sort takes a lambda for the order. Much shorter than old loops or classes.
Built-in functional interfaces
- Runnable —
() -> { ... }, code to run. - Comparator —
(a, b) -> ..., for sorting. - Predicate —
x -> boolean, a test. - Function —
x -> result, a transform.
Java provides many ready functional interfaces in java.util.function, so you rarely define your own.
Method references
// lambda: nums.forEach(n -> System.out.println(n)); // method reference (shorter): nums.forEach(System.out::println);
2
3
When a lambda only calls one existing method, you can replace it with a method reference using ::. It is the most compact form and reads cleanly.
Common mistakes
- Using a lambda on a non-functional interface. It must have exactly one abstract method.
- Forgetting
returnin a block body. Single expressions return automatically;{ }blocks do not. - Overcomplicating lambdas. For long logic, a named method is clearer.
- Wrong parameter count. It must match the interface's single method.
Summary
- A lambda is a short, unnamed function:
(params) -> body. - It works with a functional interface (one abstract method).
- Lambdas make collection code (forEach, sort) concise.
- Java provides ready functional interfaces like Predicate and Function.
- Method references (
::) are an even shorter form.
Next, process data with the Stream API, or sort with Comparator.
Frequently Asked Questions
What is a lambda expression in Java?
-> syntax. For example (x) -> x * 2. It lets you pass behaviour as a value, making code shorter and cleaner.What is a functional interface?
When were lambdas added to Java?
What is a method reference?
::, like System.out::println. It is used when the lambda only calls one method.