O'Reilly logo

Java 11 Cookbook by Mohamed Sanaulla, Nick Samoylov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required