@FunctionalInterface
interface ArithmeticFunction {
public int calcualate(int a, int b);
}
public class Main {
public static void main
(String args
[]) { ArithmeticFunction addition = (a, b) -> a + b;
ArithmeticFunction subtraction = (a, b) -> a - b;
int a = 20, b = 5;
System.
out.
println(perform
(addition, a, b
)); // or
System.
out.
println(perform
((x, y
) -> x
+ y, a, b
));
System.
out.
println(perform
(subtraction, a, b
)); // or
System.
out.
println(perform
((x, y
) -> x
- y, a, b
)); }
static int perform(ArithmeticFunction function, int a, int b) {
return function.calcualate(a, b);
}
}
QEZ1bmN0aW9uYWxJbnRlcmZhY2UKaW50ZXJmYWNlIEFyaXRobWV0aWNGdW5jdGlvbiB7CglwdWJsaWMgaW50IGNhbGN1YWxhdGUoaW50IGEsIGludCBiKTsKfQoKcHVibGljIGNsYXNzIE1haW4gewoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nIGFyZ3NbXSkgewoJCUFyaXRobWV0aWNGdW5jdGlvbiBhZGRpdGlvbiA9IChhLCBiKSAtPiBhICsgYjsKCQlBcml0aG1ldGljRnVuY3Rpb24gc3VidHJhY3Rpb24gPSAoYSwgYikgLT4gYSAtIGI7CgoJCWludCBhID0gMjAsIGIgPSA1OwoKCQlTeXN0ZW0ub3V0LnByaW50bG4ocGVyZm9ybShhZGRpdGlvbiwgYSwgYikpOwoJCS8vIG9yCgkJU3lzdGVtLm91dC5wcmludGxuKHBlcmZvcm0oKHgsIHkpIC0+IHggKyB5LCBhLCBiKSk7CgoJCVN5c3RlbS5vdXQucHJpbnRsbihwZXJmb3JtKHN1YnRyYWN0aW9uLCBhLCBiKSk7CgkJLy8gb3IKCQlTeXN0ZW0ub3V0LnByaW50bG4ocGVyZm9ybSgoeCwgeSkgLT4geCAtIHksIGEsIGIpKTsKCX0KCglzdGF0aWMgaW50IHBlcmZvcm0oQXJpdGhtZXRpY0Z1bmN0aW9uIGZ1bmN0aW9uLCBpbnQgYSwgaW50IGIpIHsKCQlyZXR1cm4gZnVuY3Rpb24uY2FsY3VhbGF0ZShhLCBiKTsKCX0KfQ==