How to do it...

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 FunctionConsumerSupplier, and Predicate interfaces.

The following are the steps you can follow to get familiar with functional interfaces:

  1. 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:

 Function<Integer, ...

Get Java 11 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.