目录
一:自定义函数
抽象出的函数
public static void main(String[] args) {
Type1 type1 = a -> (a & 1) == 0;
Type2 type2 = (a, b, c) -> a + b + c;
Type3 type3 = () -> new Student();
Type4 type4 = () -> new ArrayList<Student>();
// 利用泛型
Type5<Student> type5 = () -> new Student();
Type6<Student> type6 = student -> {student.getAge();
return student;
};
// 两个输入参数,返回一个参数
Type7<String, Student> type7 = student -> {
return student.getName();
};
}
函数式接口
Type1
/**
* 函数式接口,我们只关注参数和返回值类型
*/
@FunctionalInterface
interface Type1{
boolean op(int a);
}
Type2
@FunctionalInterface
interface Type2{
int op(int a, int b, int c);
}
Type3
@FunctionalInterface
interface Type3{
Student op();
}
Type4
@FunctionalInterface
interface Type4{
List<Student> op();
}
Type5
/**
* 利用泛型进行扩展
* @param <T>
*/
@FunctionalInterface
interface Type5<T> {
T op();
}
Type6
@FunctionalInterface
interface Type6<T> {
T op(Student student);
}
Type7
// Type6把入参也进行泛型,输入I类型返回T类型
@FunctionalInterface
interface Type7<T, I> {
T op(I input);
}
二:四大函数式接口
Function<T, R>
函数型接口
表示接收一个参数并返回结果的函数。该函数式接口方法为R apply(T t)
:
<T>
为入参泛型
<R>
为返回结果泛型
示例代码一:
Function<Integer, String> function = new Function<Integer, String>() {
@Override
public String apply(Integer integer) {
return integer.toString() + "程序员节";
}
};
System.out.println("demo1 --> " + function.apply(1024));
示例代码二:函数式接口做为方法的入参
static void functionDemo2(Integer integer, Function<Integer, String> function) {
System.out.println("demo2 --> " + function.apply(integer));
}
public static void main(String[] args) {
functionDemo4(1024, integer -> integer.toString() + "程序员节");
}
Predicate<T>
断定型接口
表示接收一个参数,判断该参数是否满足某约束,并返回boolean
值。该函数式接口方法为boolean test(T t)
。
<T>
为入参泛型
返回boolean
型值
示例代码一:
static void predicateDemo1() {
Predicate<Integer> predicate = integer -> integer == 1024;
System.out.println("predicateDemo1 --> " + predicate.test(1024));
}
示例代码二:
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>
为入参泛型
示例代码一:
static void consumerDemo1() {
Consumer<Integer> consumer = integer -> System.out.println("consumerDemo1 --> " + integer);
consumer.accept(1024);
}
示例代码二:
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));
}
示例代码三:
public static void consume(String s, Consumer<String> con1, Consumer<String> con2) {
con1.andThen(con2).accept(s);
// 完全等价于
// con1.accept(s);
// con2.accept(s);
}
public static void printer(Consumer<String> con1, Consumer<String> con2, String[] arr){
for(String s : arr){
con1.andThen(con2).accept(s);
}
}
public static void main(String[] args) {
String[] arr = {"Alice,12", "Chino,10", "Cocoa,9"};
printer((s)->{
System.out.println("姓名:" + s.split(",")[0] + ";");
}, (s)->{
System.out.println("年龄:" + s.split(",")[1] + ".\n");
}, arr);
}
输出:
姓名:Alice;
年龄:12.
姓名:Chino;
年龄:10.
姓名:Cocoa;
年龄:9.
Supplier<T>
供给型接口
与Consumer
相反,Supplier
只有返回结果,表示为提供者。该函数式接口方法为T get()
<T>
为返回结果泛型
代码示例一:
static void supplierDemo1() {
Supplier<Integer> supplier = () -> 1024;
System.out.println("supplierDemo1 --> " + supplier.get());
}
代码示例二:
static void supplierDemo2(Supplier<Integer> supplier) {
System.out.println("supplierDemo2 --> " + supplier.get());
}
public static void main(String[] args) {
supplierDemo2(() -> 1024);
}
其他函数式接口(扩展)
函数型接口
函数式接口 | 参数类型 | 返回类型 | 方法 | 用途 |
---|---|---|---|---|
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结果 |
判断型接口
函数式接口 | 参数类型 | 返回类型 | 方法 | 用途 |
---|---|---|---|---|
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结果 |
消费型接口
函数式接口 | 参数类型 | 返回类型 | 方法 | 用途 |
---|---|---|---|---|
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类型的结果 |
LongSupplier | 无 | long | long getAsLong() | 表示供给long类型的结果 |