Java Lambda

(x, y) -> x + y;
  • Parameters in parentheses
  • Arrow ->
  • Statement or block with curly braces

Java Lambda

public class Calculator {
interface IntegerMath { int operation(int a, int b); }
public int operateBinary(int a, int b, IntegerMath op) { return op.operation(a, b); }
public static void main(String... args) { Calculator myApp = new Calculator(); IntegerMath addition = (a, b) -> a + b; IntegerMath subtraction = (a, b) -> a - b; System.out.println("40 + 2 = " + myApp.operateBinary(40, 2, addition)); System.out.println("20 - 10 = " + myApp.operateBinary(20, 10, subtraction)); } }

Java Lambda

public static void processPersonsWithFunction(
    List roster,
    Predicate tester,
    Function mapper,
    Consumer block) {
    for (Person p : roster) {
        if (tester.test(p)) {
            String data = mapper.apply(p);
            block.accept(data);
        }
    }
}
processPersonsWithFunction( roster, p -> p.getGender() == Person.Sex.MALE && p.getAge() >= 18 && p.getAge() <= 25, p -> p.getEmailAddress(), email -> System.out.println(email) );