Last active
August 29, 2015 14:00
-
-
Save tomlins/b2a40543c53f86208120 to your computer and use it in GitHub Desktop.
Simple usage of Lambda expression with an in-class defined functional interface
This file contains hidden or 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
package lambdaCalculator; | |
/** | |
* | |
* @author @ectomorph | |
*/ | |
public class LambdaCalculator { | |
// In-class defined interface | |
@FunctionalInterface | |
interface IntegerMath { | |
int operation(int a, int b); | |
} | |
public int binaryOperator(int a, int b, IntegerMath op) { | |
return op.operation(a, b); | |
} | |
public static void main(String... args) { | |
LambdaCalculator myCalc = new LambdaCalculator(); | |
System.out.println("10 * 10 = " + myCalc.binaryOperator(10, 10, (a, b) -> a * b)); | |
// or, how about this? Using an in-class defined functional interface | |
IntegerMath addition = (a, b) -> a + b; | |
IntegerMath subtraction = (a, b) -> a - b; | |
System.out.println("40 + 2 = " + myCalc.binaryOperator(40, 2, addition)); | |
System.out.println("20 - 10 = " + myCalc.binaryOperator(20, 10, subtraction)); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment