文章内容整理自 博学谷狂野架构师
概述
什么是函数式接口?简单来说就是只有一个抽象函数的接口。为了使得函数式接口的定义更加规范,java8 提供了@FunctionalInterface 注解告诉编译器在编译器去检查函数式接口的合法性,以便在编译器在编译出错时给出提示。为了更加规范定义函数接口,给出如下函数式接口定义规则:
- 有且仅有一个抽象函数
- 必须要有@FunctionalInterface 注解
- 可以有默认方法
可以看出函数式接口的编写定义非常简单,不知道大家有没有注意到,其实我们经常会用到函数式接口,如Runnable 接口,它就是一个函数式接口:
COPY@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
过去我们会使用匿名内部类来实现线程的执行体:
COPYnew Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hello FunctionalInterface");
}
}).start();
现在我们使用Lambda 表达式,这里函数式接口的使用没有体现函数式编程思想,这里输出字符到标准输出流中,产生了副作用,起到了简化代码的作用,当然还有装B。
COPYnew Thread(()->{
System.out.println("Hello FunctionalInterface");
}).start();
Java8 util.function 包下自带了43个函数式接口,大体分为以下几类:
- Consumer 消费接口
- Function 功能接口
- Operator 操作接口
- Predicate 断言接口
- Supplier 生产接口
其他接口都是在此基础上变形定制化罢了。
函数式接口详细介绍
这里只介绍最基础的函数式接口,至于它的变体只要明白了基础自然就能够明白
Consumer
消费者接口,就是用来消费数据的。
COPY@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
Consumer 接口中有accept 抽象方法,accept接受一个变量,也就是说你在使用这个函数式接口的时候,给你提供了数据,你只要接收使用就可以了;andThen 是一个默认方法,接受一个Consumer 类型,当你对一个数据使用一次还不够爽的时候,你还能再使用一次,当然你其实可以爽无数次,只要一直使用andThan方法。
Function
何为Function呢?比如电视机,给你带来精神上的愉悦,但是它需要用电啊,电视它把电转换成了你荷尔蒙,这就是Function,简单电说,Function 提供一种转换功能。
COPY@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If eva luation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(