Before you create your own functional interface, consider using one of the 43 functional interfaces provided in the java.util.function package first. Most of them are specializations of the Function, Consumer, Supplier, and Predicate interfaces.
The following are the steps you can follow to get familiar with functional interfaces:
- Look at the Function<T,R> functional interface:
@FunctionalInterface public interface Function<T,R>
As you can see from the <T,R> generics, the only method of this interface takes a parameter of the T type and returns a value of the R type. According to the JavaDoc, this interface has the R apply(T t) method. We can create an implementation of this interface using an anonymous class: