Equivalent for Python's lambda functions in Ja

2019-03-17 12:40发布

Can someone please tell me if there is an equivalent for Python's lambda functions in Java?

7条回答
Evening l夕情丶
2楼-- · 2019-03-17 13:33

Somewhat similarly to Zifre's, you could create an interface thus

public interface myLambda<In, Out> {
    Out call(In i);
}

to enable you to write, say

Function<MyObj, Boolean> func = new Function<MyObj, Boolean>() {
    public Boolean callFor(myObj obj) {
        return obj.canDoStuff();
    };

MyObj thing = getThing;

if (func.callFor(thing)) {
    doSomeStuff();
} else {
    doOtherStuff();
}

It's still a bit kludgy, yeah, but it has input/output at least.

查看更多
登录 后发表回答