I'm trying to understand how lambda is work. In the process i found this example.
public class Calculator {
interface IntegerMath {
int operation(int a, int b);
default IntegerMath swap() {
return (a, b) -> operation(b, a);
}
}
private static int apply(int a, int b, IntegerMath op) {
return op.operation(a, b);
}
public static void main(String[] args) {
IntegerMath addition = (a, b) -> a + b;
Math subtraction = (a, b) -> a - b;
System.out.println(apply(5, 10, subtraction));
System.out.println(apply(5, 10, subtraction.swap()));
}
}
I'm trying to convert the swap method implementation from lambda to normal method.
But i cant change that, because if i did that i should write the implementation of operation method and create an infinity recursive.
public class Calculator {
interface IntegerMath {
int operation(int a, int b);
default IntegerMath swap() {
return new IntegerMath() {
@Override
public int operation(int a, int b) {
return operation(b, a);
}
};
}
}
}
Can i not use lambda in swap method in interface IntegerMath?