Tuesday, March 4, 2014

JAVA 8 Lambda Expression

Following example explain how Lambda Expression can be implemented in JAVA 8.

    interface MathOperation {

        int execute(int a, int b);
    }

    public int calculate(int a, int b, MathOperation op) {
        return op.execute(a, b);
    }

    public static void main(String[] args) {
        Lamda lamda = new Lamda();
        MathOperation addition = (a, b) -> (a + b);
        MathOperation substract = (a, b) -> (a - b);

        System.out.println(lamda.calculate(4, 5, addition));
        System.out.println(lamda.calculate(4, 5, substract));
    }

No comments:

Post a Comment