A lambda expression in Java is a short, anonymous function that you can pass around as a value. Introduced in Java 8, it implements the single abstract method of a functional interface without writing a class, using the syntax (parameters) -> body. Lambdas make code such as sorting, filtering and callbacks much shorter and are the foundation of the Stream API.
A lambda has no name, no return type declaration and no implements clause. The compiler infers which interface it implements from the context, such as a method parameter or an assignment target.
thisA lambda has three parts: a parameter list, the arrow token ->, and a body.
The body is either a single expression, whose value is returned automatically, or a block in braces. You cannot mix inferred and declared parameter types in one lambda, except that var can be used for all parameters.
A lambda can only be used where the target type is a functional interface: an interface with exactly one abstract method. The @FunctionalInterface annotation is optional, but it makes the compiler check that the interface stays valid.
You rarely have to write your own interface. The java.util.function package provides the common ones:
| Interface | Method | Use |
|---|---|---|
Predicate<T> | boolean test(T t) | Filtering and conditions |
Function<T, R> | R apply(T t) | Transforming a value |
Consumer<T> | void accept(T t) | Doing something with a value |
Supplier<T> | T get() | Producing a value |
BiFunction<T, U, R> | R apply(T t, U u) | Two inputs, one result |
Runnable | void run() | Code to run, for example on a thread |
Comparator<T> | int compare(T a, T b) | Ordering |
Lambdas are most useful with collections and the Stream API:
Person::name is a method reference, a shorthand for the lambda p -> p.name(). Use a method reference when the lambda only calls one existing method, and a lambda when you need extra logic. Stream.toList() is available from Java 16.
thisA lambda can read local variables from the enclosing method, but only if they are final or effectively final (never reassigned after initialisation):
Fields of the enclosing object can be read and modified freely. Inside a lambda, this refers to the enclosing instance, which differs from an anonymous inner class where this is the inner object. A lambda also does not introduce a new variable scope, so a lambda parameter cannot reuse the name of a local variable in the enclosing method.
| Aspect | Lambda | Anonymous class |
|---|---|---|
| Works for | Functional interfaces only | Any class or interface |
Meaning of this | The enclosing object | The anonymous object |
| Verbosity | Very low | High |
| Bytecode | Linked at run time with invokedynamic | Separate class file |
throws.IntPredicate, IntFunction, ToIntFunction) in hot code to avoid boxing.A method reference such as String::isEmpty is a compact form of a lambda that just calls an existing method. Both produce an instance of a functional interface.
No. It is anonymous, but you can store it in a variable of a functional interface type and reuse it through that variable.
A lambda may run later or on another thread, and local variables live on the stack of the original method. Requiring effective finality gives the lambda a safe copy of the value.
Since Java 8, released in 2014. They work in all later versions, including the current LTS releases.
A lambda expression is a concise implementation of a functional interface, written as (parameters) -> body. Use it for comparators, callbacks and stream pipelines, prefer method references for simple calls, keep captured variables effectively final, and move long bodies into named methods.