Описание тега functional-interface
A functional interface in the java language refers to an interface with a single abstract method. @FunctionalInterface
is an annotation which requires the interface to conform to this specification. The target type of a lambda expression or method reference must be a functional interface. Functional interfaces are part of the java-8 feature set.
The following is a simple functional interface:
@FunctionalInterface
interface StringFunction<T> {
String applyToString(T obj);
}
The single abstract method declaration allows it to be the target of a lambda expression:
StringFunction<Integer> toHexStringFn =
(Integer n) -> Integer.toHexString(n);
If the interface has more than one abstract method, it is not longer a functional interface and can no longer be the target of a lambda.
Functional interfaces should be annotated with the @FunctionalInterface
annotation, which requires the interface to conform to the functional interface specification. An interface annotated with @FunctionalInterface
will cause a compilation error if the interface has e.g. more than one abstract method. (This is similar to the behavior of the @Override
annotation, which will cause a compilation error if a method override is incorrect.)
See also: