I have a Consumer that I'd like to convert into a Function.
I could achieve that by using
public Function consumerToFunction(Consumer consumer)
{
return x -> {
consumer.accept(x);
return null;
};
}
But I suspect that something like that might already exist in the JDK or perhaps in a common library.
解决方案
It looks like you need to adapt a Consumer to a Function. You have created a good example of the Adapter Pattern.
[T]he adapter pattern is a software design pattern that allows the interface of an existing class to be used from another interface.
You are letting a Consumer be used from a Function.
I know of no JDK built-in converter between functional interfaces, but this appears to be a good way of applying a standard pattern to solve your problem.