假设我在Java 8中有以下功能接口:

interface Action<T, U> {
   U execute(T t);
}

在某些情况下,我需要一个没有参数或返回类型的操作。所以我写 就像这样:

Action<Void, Void> a = () -> { System.out.println("Do nothing!"); };

但是,它给了我编译错误,我需要把它写成

Action<Void, Void> a = (Void v) -> { System.out.println("Do nothing!"); return null;};

这很难看。是否有办法摆脱Void类型参数?


当前回答

在函数接口中添加静态方法

package example;

interface Action<T, U> {
       U execute(T t);
       static  Action<Void,Void> invoke(Runnable runnable){
           return (v) -> {
               runnable.run();
                return null;
            };         
       }
    }

public class Lambda {


    public static void main(String[] args) {

        Action<Void, Void> a = Action.invoke(() -> System.out.println("Do nothing!"));
        Void t = null;
        a.execute(t);
    }

}

输出

Do nothing!

其他回答

λ:

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

实际上表示接口的实现,如下所示:

public interface Something {
    void action();
}

这和你定义的完全不同。这就是为什么你会得到一个错误。

既然你不能扩展你的@FunctionalInterface,也不能引入一个全新的接口,那么我认为你没有太多的选择。不过,您可以使用Optional<T>接口来表示缺少某些值(返回类型或方法参数)。然而,这并不会使体更简单。

我认为这张表简短而有用:

Supplier       ()    -> x
Consumer       x     -> ()
BiConsumer     x, y  -> ()
Callable       ()    -> x throws ex
Runnable       ()    -> ()
Function       x     -> y
BiFunction     x,y   -> z
Predicate      x     -> boolean
UnaryOperator  x1    -> x2
BinaryOperator x1,x2 -> x3

正如在其他回答中所说,这个问题的适当选项是可运行的

如果它不需要任何东西,但返回一些东西,则使用Supplier。

如果它获取一些东西,但不返回任何东西,则使用Consumer。

如果Callable返回一个结果并且可能抛出,则使用Callable(最类似于一般CS术语中的Thunk)。

如果Runnable既不做也不能抛出,则使用Runnable。

这是不可能的。具有非Void返回类型的函数(即使它是Void)必须返回一个值。然而,你可以添加静态方法的动作,让你“创建”一个动作:

interface Action<T, U> {
   U execute(T t);

   public static Action<Void, Void> create(Runnable r) {
       return (t) -> {r.run(); return null;};
   }

   public static <T, U> Action<T, U> create(Action<T, U> action) {
       return action;
   } 
}

这将允许您编写以下内容:

// create action from Runnable
Action.create(()-> System.out.println("Hello World")).execute(null);
// create normal action
System.out.println(Action.create((Integer i) -> "number: " + i).execute(100));

在函数接口中添加静态方法

package example;

interface Action<T, U> {
       U execute(T t);
       static  Action<Void,Void> invoke(Runnable runnable){
           return (v) -> {
               runnable.run();
                return null;
            };         
       }
    }

public class Lambda {


    public static void main(String[] args) {

        Action<Void, Void> a = Action.invoke(() -> System.out.println("Do nothing!"));
        Void t = null;
        a.execute(t);
    }

}

输出

Do nothing!