Equivalent for Python's lambda functions in Java?

前端 未结 7 1478
小蘑菇
小蘑菇 2021-02-05 06:41

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

7条回答
  •  灰色年华
    2021-02-05 06:57

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

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

    to enable you to write, say

    Function func = new Function() {
        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.

提交回复
热议问题