O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

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

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

  1. Look at the functional interface Function
        @FunctionalInterface        public interface Function<T,R>

Here is its Javadoc, Accepts one argument of type T and produces result of type R. The functional method is apply(Object). You can create an implementation of this interface using an anonymous class:

        Function<Integer, Double> ourFunc = new                           Function<Integer, Double>(){          public Double apply(Integer i){            return i * 10.0;          }        };

Its only method, namely apply(), accepts the value of the type Integer (or the primitive int, which is going to be autoboxed) as a parameter, then multiplies it by 10, and returns the value of the type Double (or unboxed ...

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