I am trying to convert a Consumer
to a Runnable
. The following code does not generate any compiler errors in Eclipse IDE.
Consumer<Object> consumer;
Runnable runnable;
Object value;
...
runnable = () -> consumer.accept(value);
The following code generates a compiler error in Eclipse IDE.
ArrayList<Consumer<Object>> list;
Object value;
...
list.
stream().
map(consumer -> () -> consumer.accept(value));
The errors are:
Type mismatch: Can not convert from Stream<Object> to <unknown>.
The target type of this expression must be a functional interface.
How do I help the compiler convert a Consumer
to a Runnable
?
The following code fixes the problem but is very verbose.
map(consumer -> (Runnable) (() -> consumer.accept(value)));
Is there a more concise way to do this? I know I could create a static method which accepts a Consumer
and returns a Runnable
, but I don't think of that as more concise.