函数式接口其中有一个单一的功能,以显示出这些接口。例如,一个可比接口使用单个方法compareTo,并且被用于比较目的。 Java8定义被广泛应用于lambda表达式很多函数形式的接口。以下是在java.util.Function包中定义的功能接口列表。
S.N.
接口和说明
1
BiConsumer
表示接收两个输入参数和不返回结果的操作。
2
BiFunction
表示接受两个参数,并产生一个结果的函数。
3
BinaryOperator
表示在相同类型的两个操作数的操作,生产相同类型的操作数的结果。
4
BiPredicate
代表两个参数谓词(布尔值函数)。
5
BooleanSupplier
代表布尔值结果的提供者。
6
Consumer
表示接受一个输入参数和不返回结果的操作。
7
DoubleBinaryOperator
代表在两个double值操作数的运算,并产生一个double值结果。
8
DoubleConsumer
表示接受一个double值参数,不返回结果的操作。
9
DoubleFunction
表示接受double值参数,并产生一个结果的函数。
10
DoublePredicate
代表一个double值参数谓词(布尔值函数)。
11
DoubleSupplier
表示double值结果的提供者。
12
DoubleToIntFunction
表示接受double值参数,并产生一个int值结果的函数。
13
DoubleToLongFunction
代表接受一个double值参数,并产生一个long值结果的函数。
14
DoubleUnaryOperator
表示上产生一个double值结果的单个double值操作数的操作。
15
Function
表示接受一个参数,并产生一个结果的函数。
16
IntBinaryOperator
表示对两个int值操作数的运算,并产生一个int值结果。
17
IntConsumer
表示接受单个int值的参数并没有返回结果的操作。
18
IntFunction
表示接受一个int值参数,并产生一个结果的函数。
19
IntPredicate
表示一个整数值参数谓词(布尔值函数)。
20
IntSupplier
代表整型值的结果的提供者。
21
IntToDoubleFunction
表示接受一个int值参数,并产生一个double值结果的功能。
22
IntToLongFunction
表示接受一个int值参数,并产生一个long值结果的函数。
23
IntUnaryOperator
表示产生一个int值结果的单个int值操作数的运算。
24
LongBinaryOperator
表示在两个long值操作数的操作,并产生一个long值结果。
25
LongConsumer
表示接受一个long值参数和不返回结果的操作。
26
LongFunction
表示接受long值参数,并产生一个结果的函数。
27
LongPredicate
代表一个long值参数谓词(布尔值函数)。
28
LongSupplier
表示long值结果的提供者。
29
LongToDoubleFunction
表示接受double参数,并产生一个double值结果的函数。
30
LongToIntFunction
表示接受long值参数,并产生一个int值结果的函数。
31
LongUnaryOperator
表示上产生一个long值结果单一的long值操作数的操作。
32
ObjDoubleConsumer
表示接受对象值和double值参数,并且没有返回结果的操作。
33
ObjIntConsumer
表示接受对象值和整型值参数,并返回没有结果的操作。
34
ObjLongConsumer
表示接受对象的值和long值的说法,并没有返回结果的操作。
35
Predicate
代表一个参数谓词(布尔值函数)。
36
Supplier
表示一个提供者的结果。
37
ToDoubleBiFunction
表示接受两个参数,并产生一个double值结果的功能。
38
ToDoubleFunction
代表一个产生一个double值结果的功能。
39
ToIntBiFunction
表示接受两个参数,并产生一个int值结果的函数。
40
ToIntFunction
代表产生一个int值结果的功能。
41
ToLongBiFunction
表示接受两个参数,并产生long值结果的功能。
42
ToLongFunction
代表一个产生long值结果的功能。
43
UnaryOperator
表示上产生相同类型的操作数的结果的单个操作数的操作。
函数接口例子
谓词 Predicate 接口与方法试验(对象)返回一个布尔值功能接口。此接口意味着一个对象被检测为 true 或 false。
选择使用任何编辑器创建以下java程序在 C:/> JAVA
Java8Tester.java
importjava.util.Arrays;importjava.util.List;importjava.util.function.Predicate;publicclassJava8Tester{publicstaticvoidmain(Stringargs[]){Listlist=Arrays.asList(1,2,3,4,5,6,7,8,9);// Predicate predicate = n -> true// n is passed as parameter to test method of Predicate interface// test method will always return true no matter what value n has.System.out.println("Print all numbers:");//pass n as parametereval(list,n->true);// Predicate predicate1 = n -> n%2 == 0// n is passed as parameter to test method of Predicate interface// test method will return true if n%2 comes to be zeroSystem.out.println("Print even numbers:");eval(list,n->n%2==0);// Predicate predicate2 = n -> n > 3// n is passed as parameter to test method of Predicate interface// test method will return true if n is greater than 3.System.out.println("Print numbers greater than 3:");eval(list,n->n>3);}publicstaticvoideval(Listlist,Predicatepredicate){for(Integern:list){if(predicate.test(n)){System.out.println(n+" ");}}}}
在这里,我们使用通过谓语/Predicate 接口,需要一个单一的输入,并返回 boolean 值。
验证结果
使用javac编译器编译如下类
C:\JAVA>javac Java8Tester.java
现在运行Java8Tester看到的结果
C:\JAVA>java Java8Tester
看到结果。
Print all numbers:
1
2
3
4
5
6
7
8
9
Print even numbers:
2
4
6
8
Print numbers greater than 3:
4
5
6
7
8
9
¥ 我要打赏
纠错/补充
收藏
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。