Lambda Expression (Java)

A lambda expression is a short, anonymous block of code you can pass around like a value, written with the -> arrow. It is a concise way to provide the single method of a functional interface.

Java

Lambdas, added in Java 8, let you write behavior inline instead of creating a whole class. They are widely used with the Streams API and with interfaces like Runnable, Comparator, and Predicate.

The syntax is (parameters) -> expression or (parameters) -> { statements; }.

Examples

Java

import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<Integer, Integer> doubler = x -> x * 2;
        System.out.println(doubler.apply(5));
    }
}

Output:

10

Learn it in a lesson

See also

Back to the Dictionary