Functional Interface is an interface that has only one abstract method, but can have multiple non-abstract methods.
Functional interfaces can be implicitly converted to lambda expressions.
Functional interfaces can support lambdas friendly by existing functions.
introduce
Functional interfaces are actually an abstract interface class. The following functional interfaces have been found before Java 8.
java.lang.Runnablejava.util.concurrent.Callablejava.util.Comparator
etc...
How to use
In fact, the interface class mentioned above only needs to be modified with FunctionalInterface annotation, and becomes a functional interface in Java. For example, the Callable interface definition in JDK
@FunctionalInterfacepublic interface Callable<V> { V call() throws Exception;}It's that simple.
Now let’s talk about the new Function interface added to Java 8. Here is its definition
// T is the incoming parameter // R is the return parameter @FunctionalInterfacepublic interface Function<T, R> { R apply(T t); default <V> Function<V, R> compose(Function<? super V, ? extends T> before { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } static <T> Function<T, T> identity() { return t -> t; }}It can be understood as a function pointer in C language (personal opinion).
In actual use, the apply method is widely used. compose/andThen is mostly used in scenarios where more than two function interfaces and execution sequence is performed.
In specific business code, I generally use it in combination with BiFuncton/Supplier. BiFunction supports 2 parameters, Function supports only one parameter. Supplier can be used to store specific required values and get them through get.
example
References to the code you usually work on. This example mainly avoids multiple judgment conditions that cause bloated code. At the same time, the same business logic can be abstracted from the function interface, so that code can be reused in multiple places. The specific code is as follows.
Function<Object, Integer> actionTest1 = (object) -> { // logic return 0;};Function<Object, Integer> actionTest2 = (object) -> { // logic return 0;};public Supplier<Map<Integer, Function<Object, Integer>>> actionSupplier = () -> { Map<Integer, Function<Object, Integer>> maps = new HashMap<>(); maps.put(1, actionTest1); maps.put(2, actionTest2); return maps;};// Use public void test(int type, Object object) { Optional.ofNullable(actionSupplier.get().get(type)).ifPresent(x -> x.apply(v, object)); // if/else logic if (type == 1) { // test1 logic } else if (type == 2) { // test2 logic }}Summarize
Personally, I believe that in scenarios where there are more judgments on business logic branches, it is more suitable to use Function, and there are the following benefits.
Functional interface example
The Predicate <T> interface is a functional interface that accepts an input parameter T and returns a Boolean result.
This interface contains a variety of default methods to combine Predicate into other complex logics (such as: versus, or, non).
This interface is used to test the object is true or false.
We can understand the use of the functional interface Predicate <T> through the following example (Java8Tester.java):
Java8Tester.java file 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 test method that passes arguments to the Predicate interface // n If there is, the test method returns true System.out.println("Out all data:"); // Pass the parameter n eval(list, n->true); // Predicate<Integer> predict1 = n -> n%2 == 0 // n is a test method that passes the parameter to the Predicate interface // If n%2 is 0 test method returns true System.out.println("Out all even numbers:"); eval(list, n-> n%2 == 0); // Predicate<Integer> predict2 = n -> n > 3 // n is a test method that passes the parameter to the Predicate interface // If n is greater than 3 test method returns true System.out.println("Output all numbers greater than 3:"); eval(list, n-> n > 3); } public static void eval(List<Integer> list, Predicate<Integer> predict) { for(Integer n: list) { if(predicate.test(n)) { System.out.println(n + " "); } } }}Execute the above script and the output result is:
$ 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 all numbers greater than 3:
4
5
6
7
8
9
Summarize
The above is the Java 8 Function functional interface and functional interface examples introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!