X Tutup
# Java8函数式接口 - Java8教程 函数式接口其中有一个单一的功能,以显示出这些接口。例如,一个可比接口使用单个方法compareTo,并且被用于比较目的。 Java8定义被广泛应用于lambda表达式很多函数形式的接口。以下是在java.util.Function包中定义的功能接口列表。 | S.N. | 接口和说明 | | --- | --- | | 1 | **BiConsumer<T,U>** 表示接收两个输入参数和不返回结果的操作。 | | 2 | **BiFunction<T,U,R>** 表示接受两个参数,并产生一个结果的函数。 | | 3 | **BinaryOperator<T>** 表示在相同类型的两个操作数的操作,生产相同类型的操作数的结果。 | | 4 | **BiPredicate<T,U>** 代表两个参数谓词(布尔值函数)。 | | 5 | **BooleanSupplier** 代表布尔值结果的提供者。 | | 6 | **Consumer<T>** 表示接受一个输入参数和不返回结果的操作。 | | 7 | **DoubleBinaryOperator** 代表在两个double值操作数的运算,并产生一个double值结果。 | | 8 | **DoubleConsumer** 表示接受一个double值参数,不返回结果的操作。 | | 9 | **DoubleFunction<R>** 表示接受double值参数,并产生一个结果的函数。 | | 10 | **DoublePredicate** 代表一个double值参数谓词(布尔值函数)。 | | 11 | **DoubleSupplier** 表示double值结果的提供者。 | | 12 | **DoubleToIntFunction** 表示接受double值参数,并产生一个int值结果的函数。 | | 13 | **DoubleToLongFunction** 代表接受一个double值参数,并产生一个long值结果的函数。 | | 14 | **DoubleUnaryOperator** 表示上产生一个double值结果的单个double值操作数的操作。 | | 15 | **Function<T,R>** 表示接受一个参数,并产生一个结果的函数。 | | 16 | **IntBinaryOperator** 表示对两个int值操作数的运算,并产生一个int值结果。 | | 17 | **IntConsumer** 表示接受单个int值的参数并没有返回结果的操作。 | | 18 | **IntFunction<R>** 表示接受一个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<R>** 表示接受long值参数,并产生一个结果的函数。 | | 27 | **LongPredicate** 代表一个long值参数谓词(布尔值函数)。 | | 28 | **LongSupplier** 表示long值结果的提供者。 | | 29 | **LongToDoubleFunction** 表示接受double参数,并产生一个double值结果的函数。 | | 30 | **LongToIntFunction** 表示接受long值参数,并产生一个int值结果的函数。 | | 31 | **LongUnaryOperator** 表示上产生一个long值结果单一的long值操作数的操作。 | | 32 | **ObjDoubleConsumer<T>** 表示接受对象值和double值参数,并且没有返回结果的操作。 | | 33 | **ObjIntConsumer<T>** 表示接受对象值和整型值参数,并返回没有结果的操作。 | | 34 | **ObjLongConsumer<T>** 表示接受对象的值和long值的说法,并没有返回结果的操作。 | | 35 | **Predicate<T>** 代表一个参数谓词(布尔值函数)。 | | 36 | **Supplier<T>** 表示一个提供者的结果。 | | 37 | **ToDoubleBiFunction<T,U>** 表示接受两个参数,并产生一个double值结果的功能。 | | 38 | **ToDoubleFunction<T>** 代表一个产生一个double值结果的功能。 | | 39 | **ToIntBiFunction<T,U>** 表示接受两个参数,并产生一个int值结果的函数。 | | 40 | **ToIntFunction<T>** 代表产生一个int值结果的功能。 | | 41 | **ToLongBiFunction<T,U>** 表示接受两个参数,并产生long值结果的功能。 | | 42 | **ToLongFunction<T>** 代表一个产生long值结果的功能。 | | 43 | **UnaryOperator<T>** 表示上产生相同类型的操作数的结果的单个操作数的操作。 | ## 函数接口例子 谓词 Predicate<T> 接口与方法试验(对象)返回一个布尔值功能接口。此接口意味着一个对象被检测为 true 或 false。 选择使用任何编辑器创建以下java程序在 C:/> JAVA _Java8Tester.java_ ``` import java.util.Arrays; import java.util.List; import java.util.function.Predicate; public class Java8Tester { public static void main(String args[]){ List list = 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 parameter eval(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 zero System.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 ); } public static void eval(List list, Predicate predicate) { for(Integer n: 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 ```
X Tutup