Java 8 lambda Void argument

前端 未结 9 650
臣服心动
臣服心动 2020-12-12 08:30

Let\'s say I have the following functional interface in Java 8:

interface Action {
   U execute(T t);
}

And for some cases I ne

9条回答
  •  天涯浪人
    2020-12-12 09:18

    The lambda:

    () -> { System.out.println("Do nothing!"); };
    

    actually represents an implementation for an interface like:

    public interface Something {
        void action();
    }
    

    which is completely different than the one you've defined. That's why you get an error.

    Since you can't extend your @FunctionalInterface, nor introduce a brand new one, then I think you don't have much options. You can use the Optional interfaces to denote that some of the values (return type or method parameter) is missing, though. However, this won't make the lambda body simpler.

提交回复
热议问题