Java 8 lambda Void argument Java 8 lambda Void argument java java

Java 8 lambda Void argument


Use Supplier if it takes nothing, but returns something.

Use Consumer if it takes something, but returns nothing.

Use Callable if it returns a result and might throw (most akin to Thunk in general CS terms).

Use Runnable if it does neither and cannot throw.


I think this table is short and usefull:

Supplier       ()    -> xConsumer       x     -> ()Callable       ()    -> x throws exRunnable       ()    -> ()Function       x     -> yBiFunction     x,y   -> zPredicate      x     -> booleanUnaryOperator  x1    -> x2BinaryOperator x1,x2 -> x3

As said on the other answers, the appropriate option for this problem is a Runnable


The syntax you're after is possible with a little helper function that converts a Runnable into Action<Void, Void> (you can place it in Action for example):

public static Action<Void, Void> action(Runnable runnable) {    return (v) -> {        runnable.run();        return null;    };}// Somewhere else in your code Action<Void, Void> action = action(() -> System.out.println("foo"));