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 Function, Consumer, Supplier, 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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.