Java:一般功能X-> Y接口 [英] Java: general function X->Y interface

查看:148
本文介绍了Java:一般功能X-> Y接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  interface函数< X,Y> {
Y eval(X obj);
}

Java中是否存在类似这样的内容,或者是否需要定义我自己的?

解决方案

像这样的内置接口,尽管它只与Java 8兼容。您可以在此处找到它。



来自Javadocs:


public interface Function< ; T,R>



表示接受一个参数并产生结果的函数。 b

这是一个功能接口,其功能方法为 apply(Object)



类型参数:

T - 函数输入的类型

R - 函数结果的类型



I need an interface like:

interface Function<X,Y> {
    Y eval(X obj);
}

Is there something like this in Java already or do I need to define my own?

解决方案

There is a built-in interface like this, although it's only compatible with Java 8 onwards. You can find it here.

From the Javadocs:

public interface Function<T,R>

Represents a function that accepts one argument and produces a result.

This is a functional interface whose functional method is apply(Object).

Type Parameters:
T - the type of the input to the function
R - the type of the result of the function

这篇关于Java:一般功能X-&gt; Y接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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