本文源码:Gitee·点这里
什么是函数式接口
在介绍他们之前,我们先看看什么是函数式接口。先打开API
文档,查看FunctionalInterface
的介绍。
从API
中介绍可以看出:
@FunctionalInterface
是一个注解,旨在声明接口类型为函数式接口,切只能加在接口上。函数式接口只允许有一个抽象方法
可以包含使用
default
声明的方法,因为该方法为普通方法(Java8以后允许在接口中使用default
声明一个普通方法)可以使用
Lambada
表达式创建函数式接口的实例
注:如果接口符合函数式接口的要求,即使不加@FunctionalInterface
注解也会被视为函数式接口
四大函数式接口有哪些
从java.util.function
包的介绍可以看出,Java有四大函数式接口,分别为Function
、Consumer
、Predicate
、Supplier
,我们来逐个介绍,相信看完后你会对函数式接口有更深的理解。
Function<T, R>
函数型接口
表示接收一个参数并返回结果的函数。该函数式接口方法为R apply(T t)
:
<T>
为入参泛型<R>
为返回结果泛型
示例代码
示例1:函数式接口可以被new出一个实例来,必须实现其抽象接口
static void functionDemo1() {
Function<Integer, String> function = new Function<Integer, String>() {
@Override
public String apply(Integer integer) {
return integer.toString() + "程序员节";
}
};
System.out.println("functionDemo1 --> " + function.apply(1024));
}
示例2:可以使用Lambada表达式简化函数式接口
static void functionDemo2() {
Function<Integer, String> function = (integer) -> {
return integer.toString() + "程序员节";
};
System.out.println("functionDemo2 --> " + function.apply(1024));
}
示例3:Lambada表达式入参只有一个时,可以不加小括号;
方法体内只有一行代码时可以不加大括号和 return 关键字,直接编写返回结果即可
static void functionDemo3() {
Function<Integer, String> function = integer -> integer.toString() + "程序员节";
System.out.println("functionDemo2 --> " + function.apply(1024));
}
示例4:函数式接口做为方法的入参
static void functionDemo4(Integer integer, Function<Integer, String> function) {
System.out.println("functionDemo4 --> " + function.apply(integer));
}
public static void main(String[] args) {
functionDemo4(1024, integer -> integer.toString() + "程序员节");
}
Predicate<T>
断定型接口
表示接收一个参数,判断该参数是否满足某约束,并返回boolean
值。该函数式接口方法为boolean test(T t)
。
-
<T>
为入参泛型 - 返回
boolean
型值
示例代码
示例1:new
一个Predicate
实例
static void predicateDemo1() {
Predicate<Integer> predicate = integer -> integer == 1024;
System.out.println("predicateDemo1 --> " + predicate.test(1024));
}
示例2:Predicate
做为方法的入参
static void predicateDemo2(Integer integer, Predicate<Integer> predicate) {
System.out.println("predicateDemo1 --> " + predicate.test(integer));
}
public static void main(String[] args) {
predicateDemo2(1024, integer -> integer == 1024);
}
Consumer<T>
消费型接口
接收一个参数但不返回结果,表示为消费者。该函数式接口方法为void accept(T t)
-
<T>
为入参泛型
示例代码
示例1:new
一个Consumer
实例
static void consumerDemo1() {
Consumer<Integer> consumer = integer -> System.out.println("consumerDemo1 --> " + integer);
consumer.accept(1024);
}
示例2:Consumer
作为方法的入参
static void consumerDemo2(Integer integer, Consumer<Integer> consumer) {
consumer.accept(integer);
}
public static void main(String[] args) {
consumerDemo2(1024, integer -> System.out.println("consumerDemo2 --> " + integer));
}
Supplier<T>
供给型接口
与Consumer
相反,Supplier
只有返回结果,表示为提供者。该函数式接口方法为T get()
-
<T>
为返回结果泛型
示例代码
示例1:new
一个Supplier
实例
static void supplierDemo1() {
Supplier<Integer> supplier = () -> 1024;
System.out.println("supplierDemo1 --> " + supplier.get());
}
示例2:Supplier
作为方法的入参
static void supplierDemo2(Supplier<Integer> supplier) {
System.out.println("supplierDemo2 --> " + supplier.get());
}
public static void main(String[] args) {
supplierDemo2(() -> 1024);
}
四大函数式接口总结
为了便于记忆,我们对四大函数式接口进行一个总结:
函数式接口 | 接口类型 | 参数类型 | 返回类型 | 方法 |
---|---|---|---|---|
Function<T, R> |
函数型接口 | T | R | R apply(T t) |
Predicate<T> |
断定型接口 | T | boolean | boolean test(T t) |
Consumer<T> |
消费型接口 | T | void | void accept(T t) |
Supplier<T> |
供给型接口 | 无 | T | T get() |
其他函数式接口(扩展)
在java.util.function
包下还包含了其他函数式接口供我们使用,也是对这四大函数式接口进行的一个扩展
函数型接口
函数式接口 | 参数类型 | 返回类型 | 方法 | 用途 |
---|---|---|---|---|
BiFunction<T,U,R> |
T,U | R | R apply(T t, U u) | 接收两个输入参数并产生结果 |
ToIntBiFunction<T,U> |
T,U | int | int applyAsInt(T t,U u) | 接收两个输入参数并返回int类型结果 |
ToLongBiFunction<T,U> |
T,U | long | long applyAsLong(T t, U u) | 接收两个输入参数并返回long类型结果 |
ToDoubleBiFunction<T,U> |
T,U | double | double(T t, U u) | 接收两个输入参数并产生double类型结果 |
ToIntFunction<T> |
T | int | int applyAsInt(T value) | 接收一个参数并返回int类型结果 |
ToDoubleFunction<T> |
T | double | double applyAsDouble(T value) | 接收一个参数并返回double类型结果 |
IntFunction<R> |
int | R | R apply(int value) | 接收一个int类型参数,并返回结果 |
LongFunction<R> |
long | R | R apply(long value) | 接收一个long类型参数,并返回结果 |
DoubleFunction<R> |
double | R | R apply(double value) | 接收一个double类型参数,并返回结果 |
IntToDoubleFunction |
int | double | double applyAsDouble(int value) | 接收一个int类型参数,返回double类型结果 |
LongToDoubleFunction |
long | double | double applyAsDouble(long value) | 接收一个long类型参数,返回double类型结果 |
LongToIntFunction |
long | int | int applyAsInt(long value) | 接收一个long类型参数,返回int类型结果 |
IntToLongFunction |
int | long | long applyAsLong(int value) | 接收一个int类型参数,返回long类型结果 |
DoubleToIntFunction |
double | int | int applyAsInt(double value) | 接收一个double类型参数,返回int类型结果 |
DoubleToLongFunction |
double | long | long applyAsLong(double value) | 接收一个double类型参数,返回long类型结果 |
BinaryOperator<T> |
T,T | T | T apply(T t1, T t2) | 是BiFunction<T,T,T> 的一个子接口,表示对同一类型的两个参数进行操作,产生同一类型的结果 |
UnaryOperator<T> |
T | T | T apply(T t) | 是Function<T,T> 的一个子接口,表示对单个参数操作,产生同一类型的结果 |
LongBinaryOperator |
long,long | long | long applyAsLong(long left, long right) | 对两个long值计算并产生long结果 |
DoubleBinaryOperator |
double,double | double | double applyAsDouble(double left, double right) | 对两个double值计算并产生double结果 |
IntUnaryOperator |
int | int | int applyAsInt(int operand) | 对单个int值操作,产生一个int结果 |
LongUnaryOperator |
long | long | long applyAsLong(long operand) | 对单个long值操作,产生一个long结果 |
DoubleUnaryOperator |
double | double | double applyAsDouble(double operand) | 对单个double值操作,返回一个double结果 |
IntBinaryOperator |
int,int | int | int applyAsInt(int left, int right) | 对两个int值计算并产生int结果 |
断定型接口
函数式接口 | 参数类型 | 返回类型 | 方法 | 用途 |
---|---|---|---|---|
BiPredicate<T,U> |
T,U | boolean | boolean test(T t, U u) | 接收两个输入参数,断定并返回boolean类型结果 |
IntPredicate |
int | boolean | boolean test(int value) | 接收一个int参数值,断定并返回boolean类型结果 |
LongPredicate |
long | boolean | boolean test(long value) | 接收一个long类型参数,断定并返回boolean类型结果 |
DoublePredicate |
double | boolean | boolean test(double value) | 接收一个double参数值,断定并返回boolean类型结果 |
消费型接口
函数式接口 | 参数类型 | 返回类型 | 方法 | 用途 |
---|---|---|---|---|
BiConsumer<T,U> |
T,U | void | void accept(T t, U u) | 接收两个输入参数,不返回结果 |
ObjIntConsumer<T> |
T,int | void | void accept(T t, int value) | 接收对象值和int类型参数,不返回结果 |
ObjLongConsumer<T> |
T,long | void | void accept(T t, long value) | 接收对象值和long类型参数,不返回结果 |
ObjDoubleConsumer<T> |
T,double | void | void accept(T t, double value) | 接收对象值和double类型参数,不返回结果 |
LongConsumer |
long | void | void accept(long value) | 消费long类型值,不返回结果 |
DoubleConsumer |
double | void | void accept(double value) | 消费double类型值,不返回结果 |
IntConsumer |
int | void | void accept(int value) | 消费int类型值,不返回结果 |
供给型接口
函数式接口 | 参数类型 | 返回类型 | 方法 | 用途 |
---|---|---|---|---|
BooleanSupplier |
无 | boolean | boolean getAsBoolean() | 表示供给boolean类型的结果 |
IntSupplier |
无 | int | int getAsInt() | 表示供给int类型的结果 |
LongSupplier |
无 | long | long getAsLong() | 表示供给long类型的结果 |
DoubleSupplier |
无 | double | double getAsDouble() | 表示供给double类型的结果 |