Skip to content

Instantly share code, notes, and snippets.

@tomlins
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomlins/b2a40543c53f86208120 to your computer and use it in GitHub Desktop.
Save tomlins/b2a40543c53f86208120 to your computer and use it in GitHub Desktop.
Simple usage of Lambda expression with an in-class defined functional interface
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