Created
September 3, 2017 17:32
-
-
Save LazaroIbanez/fe7613bcebcf462f989cbd541a6d8e81 to your computer and use it in GitHub Desktop.
Java 8 Lambda Expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment