Java 8 Function Interface
Function Interface (Functional Interface) is a method of having a common interface.
Function interface can be implicitly converted to a lambda expression.
Function interface can support existing functions friendly lambda.
Before JDK 1.8 has a function interface:
- java.lang.Runnable
- java.util.concurrent.Callable
- java.security.PrivilegedAction
- java.util.Comparator
- java.io.FileFilter
- java.nio.file.PathMatcher
- java.lang.reflect.InvocationHandler
- java.beans.PropertyChangeListener
- java.awt.event.ActionListener
- javax.swing.event.ChangeListener
JDK 1.8 newly added function interface:
- java.util.function
java.util.function It contains many classes, functions to support Java programming, this package of functional interfaces:
No. | Interface and Description |
---|---|
1 | BiConsumer <T, U> It represents an operation that takes two input parameters, and does not return any results |
2 | BiFunction <T, U, R> It represents a method accepts two input parameters, and returns a result |
3 | BinaryOperator <T> Acting on behalf of an operator in two of the same type of operation, and the operator returns the same type of results |
4 | BiPredicate <T, U> It represents a boolean value method two parameters |
5 | BooleanSupplier It represents the boolean value of the result of the provider |
6 | Consumer <T> Represents accepts an input parameter and no return operation |
7 | DoubleBinaryOperator It is acting on behalf of the two double values operator operation, and returns the results of a double value. |
8 | DoubleConsumer Accepts an operation on behalf of a double value of the parameter, and does not return a result. |
9 | DoubleFunction <R> Accepting on behalf of a double parameter method, and returns the result |
10 | DoublePredicate It represents a boolean value of the parameter has a double value method |
11 | DoubleSupplier Value represents a double structure Provider |
12 | DoubleToIntFunction Accepts a double type input and returns an int result. |
13 | DoubleToLongFunction Accepts a double type input and returns a long type of result |
14 | DoubleUnaryOperator Accepts a parameter for the same type of double, return type is also double. |
15 | Function <T, R> Accepts an input parameter and returns a result. |
16 | IntBinaryOperator Accepts two parameters with the same type int, the return value type is also int. |
17 | IntConsumer Accepts an int input parameters, no return value. |
18 | IntFunction <R> Accepts an input parameter of type int, and returns a result. |
19 | IntPredicate : Accepts an int input parameter and returns a Boolean value of the result. |
20 | IntSupplier No arguments and returns an int result. |
twenty one | IntToDoubleFunction Accepts an input of type int, and returns a result of type double. |
twenty two | IntToLongFunction Accepts an input of type int, and returns a long type of result. |
twenty three | IntUnaryOperator Accepts a parameter for the same type int, the return value type is also int. |
twenty four | LongBinaryOperator It accepts two parameters with the same type of long, return type for long. |
25 | LongConsumer It takes a long type of input parameters, no return value. |
26 | LongFunction <R> To accept a long type of input parameters and returns a result. |
27 | LongPredicate R takes a long input parameter and returns a Boolean value type results. |
28 | LongSupplier No parameters and returns a result value of type long. |
29 | LongToDoubleFunction To accept a long type of input and returns a result of type double. |
30 | LongToIntFunction To accept a long type of input and returns an int result. |
31 | LongUnaryOperator It accepts a parameter for the same type of long, return type for long. |
32 | ObjDoubleConsumer <T> Accepts an object type and a double type of input parameters, no return value. |
33 | ObjIntConsumer <T> Accepts an object type and an int input parameters, no return value. |
34 | ObjLongConsumer <T> Accepts an object type and a long type of input parameters, no return value. |
35 | Predicate <T> Accepts an input parameter and returns a Boolean result. |
36 | Supplier <T> No parameters and returns a result. |
37 | ToDoubleBiFunction <T, U> It accepts two input parameters and returns a result of type double |
38 | ToDoubleFunction <T> Accepts an input parameter and returns a result of type double |
39 | ToIntBiFunction <T, U> It accepts two input parameters and returns a result of type int. |
40 | ToIntFunction <T> Accepts an input parameter and returns a result of type int. |
41 | ToLongBiFunction <T, U> It accepts two input parameters and returns a long type of result. |
42 | ToLongFunction <T> Accepts an input parameter and returns a long type of result. |
43 | UnaryOperator <T> Accept a parameter of type T, the type of the return value for T. |
Functional interface instance
Predicate <T> interface is a functional interface that accepts an input parameter T, returns a boolean result.
The interface contains several default methods will be combined into other complex Predicate logic (for example: AND, OR, NOT).
This interface is used to test objects are true or false.
We can by the following examples (Java8Tester.java) to understand the function interface Predicate <T> use:
import java.util.Arrays; import java.util.List; import java.util.function.Predicate; public class Java8Tester { public static void main(String args[]){ List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9); // Predicate<Integer> predicate = n -> true // n 是一个参数传递到 Predicate 接口的 test 方法 // n 如果存在则 test 方法返回 true System.out.println("输出所有数据:"); // 传递参数 n eval(list, n->true); // Predicate<Integer> predicate1 = n -> n%2 == 0 // n 是一个参数传递到 Predicate 接口的 test 方法 // 如果 n%2 为 0 test 方法返回 true System.out.println("输出所有偶数:"); eval(list, n-> n%2 == 0 ); // Predicate<Integer> predicate2 = n -> n > 3 // n 是一个参数传递到 Predicate 接口的 test 方法 // 如果 n 大于 3 test 方法返回 true System.out.println("输出大于 3 的所有数字:"); eval(list, n-> n > 3 ); } public static void eval(List<Integer> list, Predicate<Integer> predicate) { for(Integer n: list) { if(predicate.test(n)) { System.out.println(n + " "); } } } }
Implementation of the above script, output is:
$ javac Java8Tester.java $ java Java8Tester 输出所有数据: 1 2 3 4 5 6 7 8 9 输出所有偶数: 2 4 6 8 输出大于 3 的所有数字: 4 5 6 7 8 9