是否存在转换java.util.function的标准方法,Consumer< T>到java.util.function.Function< T,Void> [英] Is there a standard way to convert a java.util.function,Consumer<T> into a java.util.function.Function<T, Void>

查看:138
本文介绍了是否存在转换java.util.function的标准方法,Consumer< T>到java.util.function.Function< T,Void>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有消费者< T> ,我想将其转换为功能< T,Void>

I have a Consumer<T> that I'd like to convert into a Function<T, Void>.

我可以通过使用

public <T> Function<T, Void> consumerToFunction(Consumer<T> consumer)
{
    return x -> {
        consumer.accept(x);
        return null;
    };
}

但我怀疑这样的东西可能已经存在于JDK中或者可能已经存在公共图书馆。

But I suspect that something like that might already exist in the JDK or perhaps in a common library.

推荐答案

看起来您需要调整消费者< T> 功能< T,R> 。您已经创建了适配器模式的一个很好的示例。

It looks like you need to adapt a Consumer<T> to a Function<T, R>. You have created a good example of the Adapter Pattern.


[T]适配器模式是一种软件设计模式,允许从另一个接口使用现有类的接口。

[T]he adapter pattern is a software design pattern that allows the interface of an existing class to be used from another interface.

您要从功能中使用消费者

我知道功能接口之间没有JDK内置转换器,但这似乎是应用标准模式来解决问题的好方法。

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.

这篇关于是否存在转换java.util.function的标准方法,Consumer&lt; T&gt;到java.util.function.Function&lt; T,Void&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆