包路径: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);