Functional Interface

An interface with exactly one abstract method

Declare at compile time @FunctionalInterface to check validity

Lambda

A functional interface

Lambda syntax can only be combined with an instance of a functional interface

Valid syntax for lambda expression


//No Type, No Braces, No Return
(a, b) -> a - b;

//Type, No Braces, No Return
(int a) -> a++;

//No Type, With Braces, With Return
(a, b) -> { return a / b;};

//With type, With Braces, With Return
(int a, int b) -> {return a * b;};


Simple Example


public class LambdaExperiment {

  @FunctionalInterface
  interface MathOperation {
    int operation(int a, int b);
  }

     public static void main(String args[]) {
        //Instance of interface with Lambda expression specifying abstract method implementation.
        MathOperation addition = (int a, int b) -> a + b;

        //Do it (execute fuction).
        addition.operation(a, b);
    }
}


Full Example


public class LambdaExperiment {

    private static final String line = System.getProperty("line.separator").toString();
    private static final String space = " ";

    //Function Interfaces can only have one abstract method.
    @FunctionalInterface
    interface MathOperation {
        int operation(int a, int b);
    }

    public static void main(String args[]) {
        add();
        sub();
        mult();
        div();
    }

    private static void printOperation(String action, int left, int right, int test, String example, int result) {
        StringBuffer sb = new StringBuffer();

        //Simple unit test
        sb.append(example);
        sb.append(line);
        sb.append("Test case: " + left + space + action + space + right + space + "Expected: " + test + space + "Result: " + result);
        String passFail = Integer.valueOf(test).equals(result) ? "true" : "false";
        sb.append(space + "Test case " + passFail);

        System.out.println(sb.toString());
    }

    private static void add() {

        //A lambda is a functional interface with a single abstract method
        //This syntax can only be used with a functional interface
        //Overrides the single abstract method of the specified functional interface
        MathOperation addition = (int a, int b) -> a + b;
        printOperation("add", 6, 5, 11, "lambda WITH type (int a, int b) -> a + b", operate(6, 5, addition));
    }

    private static void sub() {

        //A lambda is a functional interface with a single abstract method
        //This syntax can only be used with a functional interface
        //Overrides the single abstract method of the specified functional interface
        MathOperation subtraction = (a, b) -> a - b;
        printOperation("sub", 6, 3, 3, "lambda NO type (a, b) -> a - b;", operate(6, 3, subtraction));
    }

    private static void mult() {

        //A lambda is a functional interface with a single abstract method
        //This syntax can only be used with a functional interface
        //Overrides the single abstract method of the specified functional interface
        MathOperation multiplication = (int a, int b) -> {
            return a * b;
        };
        printOperation("multi", 3, 5, 15, "lambda WITH type and with return AND curly braces (int a, int b) -> {return a * b;}", operate(3, 5, multiplication));
    }

    private static void div() {

        //A lambda is a functional interface with a single abstract method
        //This syntax can only be used with a functional interface
        //Overrides the single abstract method of the specified functional interface
        MathOperation division = (a, b) -> { return a / b;};
        printOperation("div", 100, 10, 10, "lambda NO type with return AND curly braces (a, b) -> { return a / b;}", operate(100, 10, division));
    }

    private static int operate(int a, int b, MathOperation mathOperation) {
        return mathOperation.operation(a, b);
    }
}


Inspired by: here
Examples improved on from here: here
Git it here: <3
Java 8 API Doc: here
Stackoverflow Docs: here