目录
一、概述
FunctionInterface函数接口是java8一个非常重要的新特性,它是一种指示性的类型注解,用于指示接口类型声明是由Java语言规范定义的功能接口。
从概念上讲,函数接口只有一个抽象方法。因为默认方法有一个实现,所以它们不是抽的。如果接口声明一个抽象方法覆盖Object对象的一个公共方法,则该方法也不计入接口的抽象方法计数,因为接口的任何实现都将具有来自Object对象或其他地方的实现。
请注意,可以使用lambda表达式、方法引用或构造函数引用创建函数接口的实例。
如果使用此注释类型对类型进行注释,必须满足以下两点,编译器才不会生成错误消息:
i) 类型是接口类型,而不是注释类型、枚举或类。
ii) 注释的类型满足功能接口的要求。
但是,无论接口声明中是否存在FunctionInterface注释,编译器都会将满足函数接口定义的任何接口视为函数接口。
二、四大类函数式接口
1. 消费者Consumer:代表一个输入,没有输出的操作
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
Consumer<String> c = System.out::println;
Arrays.asList("consumer","function").forEach(c);
2. 提供者Supplier:代表没有输入,提供结果的操作
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Student student = studentService.getStudentById(1);
Supplier<Student> supplier = Student::new;
Optional.ofNullable(student).orElseGet(supplier);
3. Function:代表接受一个参数,返回一个结果的操作
并且提供了identity静态方法,总是返回输入值。
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
static <T> Function<T, T> identity() {
return t -> t;
}
}
Function<String, Integer> toInt = Integer::valueOf;
Integer a = toInt.apply("111");
4. 断言者Predicate:代表接受一个参数,返回true或者false的判断操作
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
Predicate<String> predicate = String::isEmpty;
boolean a = predicate.apply("111");
三、其他重要函数接口
1. BiFunction:代表接受两个参数,返回一个值的操作
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
}
List<String> list = Arrays.asList("a", "b", "c");
BiFunction<Integer, String, String> b = list::set;
b.apply(1,"d");
b.apply(2,"f");
2.BinaryOperator:BiFunction的特例,接受的两个参数和返回值参数类型相同
常用做二分合并操作。并提供了两个重要静态方法,获取两个实现Comparator接口的最大值和最小值。jdk在stream中的Collectors收集器使用了这两个静态方法。
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
}
public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
}
}
BinaryOperator<Integer> b = Integer::sum;
Integer sum = b.apply(1,2);
public static <T> Collector<T, ?, Optional<T>>
maxBy(Comparator<? super T> comparator) {
return reducing(BinaryOperator.maxBy(comparator));
}
public static <T> Collector<T, ?, Optional<T>>
minBy(Comparator<? super T> comparator) {
return reducing(BinaryOperator.minBy(comparator));
}
3. DoubleBinaryOperator,IntBinaryOperator,LongBinaryOperator
这三个函数接口都是 BinaryOperator接口的原始类型实现,单独声明3个接口是为了避免int,double,long类型的拆装箱操作。
@FunctionalInterface
public interface DoubleBinaryOperator {
double applyAsDouble(double left, double right);
}
@FunctionalInterface
public interface IntBinaryOperator {
int applyAsInt(int left, int right);
}
@FunctionalInterface
public interface LongBinaryOperator {
long applyAsLong(long left, long right);
}
DoubleBinaryOperator accumulator = (a, b) -> a > b ? a : b;
boolean foundAny = false;
double result = 0;
for (double element : Arrays.asList(1.2d, 1.1d, 1.3d, 2d)) {
if (!foundAny) {
foundAny = true;
result = element;
} else {
result = accumulator.applyAsDouble(result, element);
}
}
return result;
在jdk8中,官方DoubleStream(IntStream,LongStrem)流处理中使用了DoubleBinaryOperator(IntBinaryOperator,LongBinaryOperator)对应的函数接口,其代码如下:
public interface DoubleStream extends BaseStream<Double, DoubleStream> {
//strem流的reduce操作,将double的stream流合并成一个double数据
OptionalDouble reduce(DoubleBinaryOperator op);
//strem流的reduce操作,提供初始值,将double的stream流合并成一个double数据
double reduce(double identity, DoubleBinaryOperator op);
}
具体的实现逻辑在DoublePipeline中:
abstract class DoublePipeline<E_IN>
extends AbstractPipeline<E_IN, Double, DoubleStream>
implements DoubleStream {
@Override
public final double reduce(double identity, DoubleBinaryOperator op) {
return evaluate(ReduceOps.makeDouble(identity, op));
}
@Override
public final OptionalDouble reduce(DoubleBinaryOperator op) {
return evaluate(ReduceOps.makeDouble(op));
}
}
此为reduce终止操作,定义TerminalOp<Double, Double>对象:
public static TerminalOp<Double, Double>
makeDouble(double identity, DoubleBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink
implements AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble {
private double state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(double t) {
state = operator.applyAsDouble(state, t);
}
@Override
public Double get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Double, Double, ReducingSink>(StreamShape.DOUBLE_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}