About
Functional Programming - Functional Interface in Java.
A functional interface is is a interface that contains only one abstract method.
The interface is so simple that java defines several standard (built-in) functional interfaces
Articles Related
Usage
Functional interfaces represent abstract concepts like:
- functions,
- actions,
- or predicates.
Structure
A functional interface is is a interface that contains only one abstract method.
The single abstract method is called the functional method.
It may contain one or more:
- or static methods.
Annotation
The FunctionalInterface annotation is an aid to capture design intent not a requirement. Compilers may generate an error.
Method
The single abstract method in a function interface is called the functional method.
Creation
Implementation of functional interfaces can be created with:
- lambda expressions, Because a functional interface contains only one abstract method, you omit the name of that method when you implement it with a lambda expression.
- or constructor references.
Standard
The predefined/standard/built-in function interface can find in the package java.util.function package
They are generic interface.
All standard interface follows the following naming convention.
- Supplier (nilary function to R).
- Predicate (unary function from T to boolean), abstract method that returns a boolean (Example: filtering)
interface Predicate<T> {
boolean test(T t);
}
- Consumer (unary function from T to void), abstract method that returns no value (Example: print)
interface Consumer<T> {
void accept(T t);
}
- Function (unary function from T to R), - abstract method that returns a value (Example: retrieve information or validation)
interface Function<T> {
R apply(T t);
}
- UnaryOperator (unary function from T to T), - abstract method that return always returns its input argument (handy for chaining)
interface UnaryOperator<T> {
T apply(T t);
}