函数式接口
包路径:java.util.funciont
特点:
- 具有唯一的抽象方法,有且仅有一个 (即所有的函数式接口,有且只能有一个抽象方法)
- 加上标注
@FunctionalInterface
,则会触发JavaCompiler
的检查。对于符合函数接口的接口,加不加都无关紧要,但是加上则会提供一层编译检查的保障。如果不符合,则会报错。 - 继承后的抽象方法则不算抽象方法。例如接口实现了
Object
中的toString()
等方法。 - 可用于lambda类型的使用方式
重点接口
- Predicate:接收参数
T
对象,返回一个boolean
值
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
- Consumer:接收参数
T
对象,没有返回对象
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
- Function:接收参数
T
对象,返回R
对象
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
- Supplier:不接收任何参数,直接通过
get()
方法获取指定类型对象
/**
* Gets a result.
*
* @return a result
*/
T get();
- UnaryOperator:接收参数
T
对象,执行业务处理后,返回更新后的T对象
继承自:
Function
,通过下面的identity()
方法,保证返回对象与接收对象一致
/**
* Returns a unary operator that always returns its input argument.
*
* @param <T> the type of the input and output of the operator
* @return a unary operator that always returns its input argument
*/
static <T> UnaryOperator<T> identity() {
return t -> t;
}
- BinaryOperator:接收两个对象
T
和U
,执行业务处理后,返回一个R
对象
继承自:
BiFunction
,下面是继承方法
/**
* Applies this function to the given arguments.
*
* @param t the first function argument
* @param u the second function argument
* @return the function result
*/
R apply(T t, U u);
-----------------------------------------------------------
个人网址:http://wxson.cn(待开通)
-----------------------------------------------------------
个人网址:http://wxson.cn(待开通)
-----------------------------------------------------------