Is there a standard way to convert a java.util.function,Consumer into a java.util.function.Function

前端 未结 2 505
夕颜
夕颜 2021-02-12 22:28

I have a Consumer that I\'d like to convert into a Function.

I could achieve that by using

public 

        
2条回答
  •  耶瑟儿~
    2021-02-12 23:04

    A standard way doesn't exist, but this should do the trick:

    Function adapt(final Consumer consumer) {
        return t -> {
            consumer.accept(t);
            return null;
        };
    }
    

    This is following the Adapter Pattern mentioned in the accepted answer from @rgettman.

提交回复
热议问题