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) -> {...});
Transforming a list
return list.stream().map( this::buildItem).collect( Collectors.toList());
Conditional test for non-null value
Invoking a method on the variable with no return value
opt.ifPresent(this::print);
Passing the (non-null) variable into method with no return value
opt.ifPresent(x -> print(x));
Passing the (non-null) variable into method and returning a result (and with value for null)
opt.map(y -> y ^ 2).orElse( -1);