An interface that has only one abstract method and can be an assignment destination for passing lambda expressions and method references.
**-Can have only one abstract method -Abstract methods that are public methods of Object class are not included -It is possible to have multiple default methods and static methods **
The definition of the functional interface is written as follows.
main.java
@FunctionalInterface
public interface Sample {
public abstract void Hello(String name);
}
Annotating ** @ FunctionalInterface ** makes it clear that it is a functional interface, which allows you to issue a compile error if the interface does not meet the requirements of a functional interface.
The java.util.function package provides several types of functional interfaces. It can be roughly divided into 4 types according to the argument to be received and the pattern of the result to be returned. The basic functional interface is as follows:
type | Abstract method | What you can do |
---|---|---|
Supplier |
T get() | Returns a T-type return value with no arguments |
Consumer |
void accept(T t) | Returns processing (no value is returned). |
Function<T, R> | R apply(T t) | Takes T as an argument and returns R as a result |
Predicate |
boolean test(T t) | Takes a T-type argument and returns a boolean value as a result |
In addition, there are other types such as an interface when there are two arguments, so use them properly according to the purpose.
Recommended Posts