The concept of functional interface is added to Java 8. And added a new annotation @FunctionalInterfaceI
1 What is the functional interface function interface?
Functional Interface:functional interface only has 1 abstract method. This is the definition of the function interface. Simply understood, the function interface is actually an interface of an abstract method (interface).
2 Types of functional interfaces
Here I only introduce a few types of basic predicate<t>,consumer<t>,function<t, r>,supplier<t>
These functions are declared in the Java.util.function package, you can see the code on their own.
1 predicate<t>--receives the T (generics) Generic object and returns a Boolean
Boolean Test (T T);
Often used for judgment, e.g. Stream.filter (predicate<t>)
2 consumer<t>--receive Tgenerics) generic object, no return value
void Accept (T t)
Used as Consumer such as foreach (consumer<t>)
3 Function<t, r>--receive tgenerics) generic object, return R (generics) Generic Object
T-R
R apply (T t)
Example Stream.map (function<t, r>)
4 supplier<t>--provides tgenerics) generic objects (such as factories) that do not receive values
() T
3. Attention to Detail
The function interface does not allow only one method, because Java 8 adds a new default method. Therefore, a functional interface can also have multiple default methods. However, a functional interface can have only one abstract method.
This article is from the "Development" blog, make sure to keep this source http://jamesdev.blog.51cto.com/2066624/1858763
Java8 lambda-functioanl Interface-function interface