English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A functional interface (Functional Interface) is an interface that has exactly one abstract method but can have multiple non-abstract methods.
Functional interfaces can be implicitly converted to lambda expressions.
Lambda expressions and method references (which can also be considered as Lambda expressions).
As defined by a functional interface as follows:
@FunctionalInterface interface GreetingService { void sayMessage(String message); }
Then you can use Lambda expressions to represent an implementation of the interface (note: JAVA 8 Previously, it was usually implemented using anonymous classes):
GreetingService greetService1 Equal message -System.out.println("Hello "}} + message);
Functional interfaces can support lambda in an existing function in a friendly manner.
JDK 1.8 Existing functional interfaces before:
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 functional interfaces:
java.util.function
java.util.function It contains many classes to support Java's functional programming, the functional interfaces in this package include:
Serial Number | Interface & Description |
---|---|
1 | BiConsumer<T,U> Represents an operation that accepts two input parameters and does not return anything |
2 | BiFunction<T,U,R> Represents a method that accepts two input parameters and returns a result |
3 | BinaryOperator<T> Represents an operation that acts on two operands of the same type and returns a result of the same type |
4 | BiPredicate<T,U> Represents a boolean value method with two parameters |
5 | BooleanSupplier Represents a supplier of a boolean value result |
6 | Consumer<T> Represents an operation that accepts an input parameter and does not return anything |
7 | DoubleBinaryOperator Represents an operation that acts on two double value operands and returns a double value result. |
8 | DoubleConsumer Represents an operation that accepts a double value parameter and does not return a result. |
9 | DoubleFunction<R> Represents a method that accepts a double value parameter and returns a result |
10 | DoublePredicate Represents a boolean value method with a double value parameter |
11 | DoubleSupplier Represents a supplier of a double value structure |
12 | DoubleToIntFunction Accepts a double type input and returns an int type result. |
13 | DoubleToLongFunction Accepts a double type input and returns a long type result. |
14 | DoubleUnaryOperator Accepts a parameter of type double, and the return type is also double. |
15 | Function<T,R> Accepts an input parameter and returns a result. |
16 | IntBinaryOperator Accepts two parameters of type int, and the return type is also int. |
17 | IntConsumer Accepts an int type input parameter, with no return value. |
18 | IntFunction<R> Accepts an int type input parameter and returns a result. |
19 | IntPredicate Accepts an int input parameter and returns a boolean type result. |
20 | IntSupplier No parameters, returns an int type result. |
21 | IntToDoubleFunction Accepts an int type input and returns a double type result. |
22 | IntToLongFunction Accepts an int type input and returns a long type result. |
23 | IntUnaryOperator Accepts a parameter of type int, and the return type is also int. |
24 | LongBinaryOperator Accepts two parameters of type long, and the return type is also long. |
25 | LongConsumer Accepts a long type input parameter, with no return value. |
26 | LongFunction<R> Accepts a long type input parameter and returns a result. |
27 | LongPredicate R accepts a long input parameter and returns a boolean type result. |
28 | LongSupplier No parameters, returns a result of type long. |
29 | LongToDoubleFunction Accepts a long type input and returns a double type result. |
30 | LongToIntFunction Accepts a long type input and returns an int type result. |
31 | LongUnaryOperator Accepts a parameter of type long and returns a long type value. |
32 | ObjDoubleConsumer<T> Accepts an object type and a double type input parameter, with no return value. |
33 | ObjIntConsumer<T> Accepts an object type and an int type input parameter, with no return value. |
34 | ObjLongConsumer<T> Accepts an object type and a long type input parameter, and has no return value. |
35 | Predicate<T> Accepts an input parameter and returns a boolean result. |
36 | Supplier<T> No parameters, returns a result. |
37 | ToDoubleBiFunction<T,U> Accepts two input parameters and returns a double type result |
38 | ToDoubleFunction<T> Accepts an input parameter and returns a double type result |
39 | ToIntBiFunction<T,U> Accepts two input parameters and returns an int type result. |
40 | ToIntFunction<T> Accepts an input parameter and returns an int type result. |
41 | ToLongBiFunction<T,U> Accepts two input parameters and returns a long type result. |
42 | ToLongFunction<T> Accepts an input parameter and returns a long type result. |
43 | UnaryOperator<T> Accepts a parameter of type T, and the return type is also T. |
The Predicate <T> interface is a functional interface that accepts an input parameter T and returns a boolean result.
This interface includes various default methods to combine Predicate into other complex logic (such as: and, or, not).
This interface is used to test whether an object is true or false.
We can use the following examples (Java8To learn about the usage of the functional interface Predicate <T> in Tester.java):
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 is a parameter passed to the test method of the Predicate interface // If n exists, the test method returns true System.out.println("Output all data:"); // Pass the parameter n eval(list, n->true); // Predicate<Integer> predicate1 = n -> n %2 == 0 // n is a parameter passed to the test method of the Predicate interface // If n%2 For 0, the test method returns true System.out.println("Output all even numbers:"); eval(list, n-> n %2 == 0 ); // Predicate<Integer> predicate2 = n -> n > 3 // n is a parameter passed to the test method of the Predicate interface // If n is greater than 3 test method returns true System.out.println("Output Numbers Greater Than 3 All Numbers: "); 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 + " "); } } } }
Execute the above script, the output will be:
$ javac Java8Tester.java $ java Java8Tester Output All Data: 1 2 3 4 5 6 7 8 9 Output All Even Numbers: 2 4 6 8 Output Numbers Greater Than 3 All Numbers: 4 5 6 7 8 9