Syntax
(parameters) -> expressionExamples
Basic Lambda Syntax
Comparing a lambda to the equivalent anonymous class it replaces.
interface Greeter {
String greet(String name);
}
// Old way: anonymous class
Greeter oldStyle = new Greeter() {
@Override
public String greet(String name) {
return "Hello, " + name;
}
};
// New way: lambda expression
Greeter newStyle = name -> "Hello, " + name;
System.out.println(newStyle.greet("John")); // Hello, JohnLambdas with Built-in Functional Interfaces
java.util.function provides ready-made functional interfaces like Function, Predicate, and Consumer.
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Consumer;
Function<Integer, Integer> square = x -> x * x;
System.out.println(square.apply(5)); // 25
Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println(isEven.test(4)); // true
Consumer<String> printer = s -> System.out.println("Value: " + s);
printer.accept("Hello"); // Value: HelloMulti-Statement Lambdas
A lambda body can contain multiple statements using curly braces and an explicit return.
import java.util.function.Function;
Function<Integer, String> classify = n -> {
if (n < 0) {
return "negative";
} else if (n == 0) {
return "zero";
} else {
return "positive";
}
};
System.out.println(classify.apply(-5)); // negative
System.out.println(classify.apply(0)); // zeroLambdas with sorting
A very common use: passing a lambda as a Comparator to control sort order.
import java.util.ArrayList;
ArrayList<String> names = new ArrayList<>(java.util.List.of("Charlie", "Alice", "Bob"));
names.sort((a, b) -> a.compareTo(b));
System.out.println(names); // [Alice, Bob, Charlie]
names.sort((a, b) -> b.length() - a.length()); // longest name first
System.out.println(names);Best practices
- Use lambdas for short, simple implementations of a functional interface - switch to a full method or class once the logic gets long or complex
- Prefer built-in functional interfaces from java.util.function (Function, Predicate, Consumer, Supplier) over defining your own where possible
- Keep lambda parameter names short but meaningful, especially in stream chains where context is often clear from position
- Remember a lambda can only implement an interface with exactly one abstract method (a functional interface) - not any arbitrary interface
Runtime & versions
Java Virtual Machine. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Java Language Specification