Java Lambda Syntax

Enjoy this cheat sheet at its fullest within Dash, the macOS documentation browser.

Java Lambda Syntax

As an expression

(Person p) -> p.getGender() == Person.Sex.MALE
    && p.getAge() >= 18
    && p.getAge() <= 25

As a local variable

@FunctionalInterface
public interface SomeFunction {
    public void doWork();
}

SomeFunction lambdaName = () -> {...};

As a method parameter

@FunctionalInterface
public interface SomeFunction {
    public void doWork();
}

void someMethodThatTakesALambda(SomeFunction lambdaName) {...}

As an argument to a method call

someObject.someMethodThatTakesALambda ((ParameterClass p) -> {...});

Notes