#Java# Functional Interface

functional interface是Java 8里面引入的一个概念。

定义

如果一个interface只含有一个抽象函数,那么这个interface就是functional interface。注解@FuntionalInterface可以写,也可以不写。比如:

@FunctionalInterface
public interface MyFunctional<T> {
    public T func(T t);
}

用法

functional interface最重要的用法是配合lambda表达式使用。lambda的概念其他很多编程语言已经广泛使用了,它的使用能很大程度上简化函数的定义,尤其是当这个函数功能简单而且与程序整体结构无关时。

Java 8 中的lambda有两种使用方法:

  • (parameters)->expression
  • (parameters)->{statements;}

parameters表示参数列表,expression是表达式,statements是代码执行块。

这个语句的含义是:定义一个函数,这个函数传入parameters作为参数,然后执行后面的expression或者statements,如果是expression的话会直接将表达式的值返回,而{statements}里面则需要显式调用return(当需要返回值时)。这两种表达式都返回一个functional interface对象,因此一般的使用情况是:

MyFunctional<String> mf = (String str) -> str+"Hello everyone!"
//等同于
MyFunctional<String> mf = (String str) -> {return str+"Hello everyone!";};

这个函数定义好了以后,调用方式则是使用functional interface里面的那个抽象函数,因此需要确保那个抽象函数的参数和调用时传入的参数(数量、类型)相符合,返回值也是。调用时的例子为:

system.out.println(mf.func("Hello world"));

执行结果为:

Hello world!Hello everyone!

实际上就是简化了代码。如果不使用lambda的话,上面的代码相当于:

MyFunctional<String> mfImpl = new MyFunctional<String>() {

    @Override
    public String func(String t) {
        return t + "Hello everyone!";

    }
};
system.out.println(mfImpl.func("Hello world!"));

Java 8提供的functional interfaces

Package java.util.function

Java和其他一些面向对象(比如python)的区别之一是,定义函数时需要定义参数类型、返回值类型等,所以使用接口这种方式来定义lambda函数时就不得不根据需要来调用不同的接口(不同参数类型、数量以及返回值等)。为了达到这样的效果,Java 8内置了一些这样的接口并放在function包里,避免开发者需要使用时自己再写一遍。记这么多还不如自己写一遍。

consumer

接口名称中含consumer的都表示不返回任何值的。

interfacedescription
Consumeraccepts a single input argument and returns no result
BiConsumeraccepts two input arguments and returns no result
DoubleConsumeraccepts a single double-valued argument and returns no result
IntConsumeraccepts a single int-valued argument and returns no result
LongConsumeraccepts a single long-valued argument and returns no result
ObjDoubleConsumeraccepts an object-valued and a double-valued argument, and returns no result
ObjIntConsumeraccepts an object-valued and a int-valued argument, and returns no result
ObjLongConsumeraccepts an object-valued and a long-valued argument, and returns no result

function

接口名称中包含function的都表示有一个返回值的。

interfacedescription
Functionaccepts one argument and produces a result
BiFunctionaccepts two arguments and produces a result
DoubleFunctionaccepts a double-valued argument and produces a result
IntFunctionaccepts an int-valued argument and produces a result
LongFunctionaccepts a long-valued argument and produces a result
DoubleToIntFunctionaccepts a double-valued argument and produces an int-valued result
DoubleToLongFunctionaccepts a double-valued argument and produces a long-valued result
IntToDoubleFunctionaccepts an int-valued argument and produces a double-valued result
IntToLongFunctionaccepts an int-valued argument and produces a long-valued result
LongToDoubleFunctionaccepts an long-valued argument and produces a double-valued result
LongToIntFunctionaccepts an long-valued argument and produces a int-valued result
ToDoubleBiFunctionaccepts two arguments and produces a double-valued result
ToIntBiFunctionaccepts two arguments and produces a int-valued result
ToLongBiFunctionaccepts two arguments and produces a long-valued result
ToDoubleFunctionproduces a double-valued result
ToIntFunctionproduces an int-valued result
ToLongFunctionproduces a long-valued result

predicate

接口名称中包含predicate的都表示返回值为boolean类型的。

interfacedescription
Predicatea predicate (boolean-valued function) of one argument
BiPredicatea predicate (boolean-valued function) of two arguments
DoublePredicatea predicate (boolean-valued function) of one double-valued argument
IntPredicatea predicate (boolean-valued function) of one int-valued argument
LongPredicatea predicate (boolean-valued function) of one long-valued argument

operator

接口名称中包含operator的都表示一个返回值,且返回值类型和参数类型相同的的。

interfacedescription
BinaryOperatoran operation upon two operands of the same type, producing a result of the same type as the operands
UnaryOperatoran operation on a single operand that produces a result of the same type as its operand
DoubleBinaryOperatoran operation upon two double-valued operands and producing a double-valued result
IntBinaryOperatoran operation upon two int-valued operands and producing a int-valued result
LongBinaryOperatoran operation upon two long-valued operands and producing a long-valued result
DoubleUnaryOperatoran operation on a single double-valued operand that produces a double-valued result
IntUnaryOperatoran operation on a single int-valued operand that produces a int-valued result
LongUnaryOperatoran operation on a single long-valued operand that produces a long-valued result

supplier

接口名称中包含supplier的都表示有不接受参数,只返回一个值的。

interfacedescription
SupplierRepresents a supplier of results.
IntSupplierRepresents a supplier of int-valued results.
LongSupplierRepresents a predicate (boolean-valued function) of one long-valued argument.
DoubleSupplierRepresents a predicate (boolean-valued function) of one double-valued argument.
BooleanSupplierRepresents a supplier of boolean-valued results.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值