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
Java和其他一些面向对象(比如python)的区别之一是,定义函数时需要定义参数类型、返回值类型等,所以使用接口这种方式来定义lambda函数时就不得不根据需要来调用不同的接口(不同参数类型、数量以及返回值等)。为了达到这样的效果,Java 8内置了一些这样的接口并放在function包里,避免开发者需要使用时自己再写一遍。记这么多还不如自己写一遍。
consumer
接口名称中含consumer的都表示不返回任何值的。
interface | description |
---|---|
Consumer | accepts a single input argument and returns no result |
BiConsumer | accepts two input arguments and returns no result |
DoubleConsumer | accepts a single double -valued argument and returns no result |
IntConsumer | accepts a single int -valued argument and returns no result |
LongConsumer | accepts a single long -valued argument and returns no result |
ObjDoubleConsumer | accepts an object-valued and a double -valued argument, and returns no result |
ObjIntConsumer | accepts an object-valued and a int -valued argument, and returns no result |
ObjLongConsumer | accepts an object-valued and a long -valued argument, and returns no result |
function
接口名称中包含function的都表示有一个返回值的。
interface | description |
---|---|
Function | accepts one argument and produces a result |
BiFunction | accepts two arguments and produces a result |
DoubleFunction | accepts a double-valued argument and produces a result |
IntFunction | accepts an int-valued argument and produces a result |
LongFunction | accepts a long-valued argument and produces a result |
DoubleToIntFunction | accepts a double-valued argument and produces an int-valued result |
DoubleToLongFunction | accepts a double-valued argument and produces a long-valued result |
IntToDoubleFunction | accepts an int-valued argument and produces a double-valued result |
IntToLongFunction | accepts an int-valued argument and produces a long-valued result |
LongToDoubleFunction | accepts an long-valued argument and produces a double-valued result |
LongToIntFunction | accepts an long-valued argument and produces a int-valued result |
ToDoubleBiFunction | accepts two arguments and produces a double-valued result |
ToIntBiFunction | accepts two arguments and produces a int-valued result |
ToLongBiFunction | accepts two arguments and produces a long-valued result |
ToDoubleFunction | produces a double-valued result |
ToIntFunction | produces an int-valued result |
ToLongFunction | produces a long-valued result |
predicate
接口名称中包含predicate的都表示返回值为boolean类型的。
interface | description |
---|---|
Predicate | a predicate (boolean-valued function) of one argument |
BiPredicate | a predicate (boolean-valued function) of two arguments |
DoublePredicate | a predicate (boolean-valued function) of one double -valued argument |
IntPredicate | a predicate (boolean-valued function) of one int -valued argument |
LongPredicate | a predicate (boolean-valued function) of one long -valued argument |
operator
接口名称中包含operator的都表示一个返回值,且返回值类型和参数类型相同的的。
interface | description |
---|---|
BinaryOperator | an operation upon two operands of the same type, producing a result of the same type as the operands |
UnaryOperator | an operation on a single operand that produces a result of the same type as its operand |
DoubleBinaryOperator | an operation upon two double -valued operands and producing a double -valued result |
IntBinaryOperator | an operation upon two int -valued operands and producing a int -valued result |
LongBinaryOperator | an operation upon two long -valued operands and producing a long -valued result |
DoubleUnaryOperator | an operation on a single double -valued operand that produces a double -valued result |
IntUnaryOperator | an operation on a single int -valued operand that produces a int -valued result |
LongUnaryOperator | an operation on a single long -valued operand that produces a long -valued result |
supplier
接口名称中包含supplier的都表示有不接受参数,只返回一个值的。
interface | description |
---|---|
Supplier | Represents a supplier of results. |
IntSupplier | Represents a supplier of int -valued results. |
LongSupplier | Represents a predicate (boolean-valued function) of one long -valued argument. |
DoubleSupplier | Represents a predicate (boolean-valued function) of one double -valued argument. |
BooleanSupplier | Represents a supplier of boolean -valued results. |