功能界面具有单一功能.例如,具有单个方法'compareTo'的Comparable接口用于比较目的. Java 8定义了许多在lambda表达式中广泛使用的函数接口.以下是java.util.Function包中定义的功能接口列表.
BiConsumer Represents an operation that accepts two input arguments, and returns no result. BiFunction Represents a function that accepts two arguments and produces a result. BinaryOperator Represents an operation upon two operands of the same type, producing a result of the same type as the operands. BiPredicate Represents a predicate (Boolean-valued function) of two arguments. BooleanSupplier Represents a supplier of Boolean-valued results. Consumer Represents an operation that accepts a single input argument and returns no result. DoubleBinaryOperator Represents an operation upon two double-valued operands and producing a double-valued result. DoubleConsumer Represents an operation that accepts a single double-valued argument and returns no result. DoubleFunction Represents a function that accepts a double-valued argument and produces a result. DoublePredicate Represents a predicate (Boolean-valued function) of one double-valued argument. DoubleSupplier Represents a supplier of double-valued results. DoubleToIntFunction Represents a function that accepts a double-valued argument and produces an int-valued result. DoubleToLongFunction Represents a function that accepts a double-valued argument and produces a long-valued result. DoubleUnaryOperator Represents an operation on a single double-valued operand that produces a double-valued result. Function Represents a function that accepts one argument and produces a result. IntBinaryOperator Represents an operation upon two int-valued operands and produces an int-valued result. IntConsumer Represents an operation that accepts a single int-valued argument and returns no result. IntFunction Represents a function that accepts an int-valued argument and produces a result. IntPredicate Represents a predicate (Boolean-valued function) of one int-valued argument. IntSupplier Represents a supplier of int-valued results. IntToDoubleFunction Represents a function that accepts an int-valued argument and produces a double-valued result. IntToLongFunction Represents a function that accepts an int-valued argument and produces a long-valued result. IntUnaryOperator Represents an operation on a single int-valued operand that produces an int-valued result. LongBinaryOperator Represents an operation upon two long-valued operands and produces a long-valued result. LongConsumer Represents an operation that accepts a single long-valued argument and returns no result. LongFunction Represents a function that accepts a long-valued argument and produces a result. LongPredicate Represents a predicate (Boolean-valued function) of one long-valued argument. LongSupplier Represents a supplier of long-valued results. LongToDoubleFunction Represents a function that accepts a long-valued argument and produces a double-valued result. LongToIntFunction Represents a function that accepts a long-valued argument and produces an int-valued result. LongUnaryOperator Represents an operation on a single long-valued operand that produces a long-valued result. ObjDoubleConsumer Represents an operation that accepts an object-valued and a double-valued argument, and returns no result. ObjIntConsumer Represents an operation that accepts an object-valued and an int-valued argument, and returns no result. ObjLongConsumer Represents an operation that accepts an object-valued and a long-valued argument, and returns no result. Predicate Represents a predicate (Boolean-valued function) of one argument. Supplier Represents a supplier of results. ToDoubleBiFunction Represents a function that accepts two arguments and produces a double-valued result. ToDoubleFunction Represents a function that produces a double-valued result. ToIntBiFunction Represents a function that accepts two arguments and produces an int-valued result. ToIntFunction Represents a function that produces an int-valued result. ToLongBiFunction Represents a function that accepts two arguments and produces a long-valued result. ToLongFunction Represents a function that produces a long-valued result. UnaryOperator Represents an operation on a single operand that produces a result of the same type as its operand.Sr.No. Interface & Description 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
功能接口示例
谓词< T> interface是一个带有方法test(Object)的函数接口,用于返回一个布尔值.此接口表示对象被测试为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[]) { 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 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接口,需要一个输入并返回布尔值.
验证结果
使用 javac 编译器编译类,如下 :
C:\JAVA>javac Java8Tester.java
现在运行Java8Tester如下 :
C:\JAVA>java Java8Tester
它应该产生以下输出 :
Print all numbers:123456789Print even numbers:2468Print numbers greater than 3:456789