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

There's more...

Many of the functional interfaces in the java.util.function package have default methods that not only enhance their functionality, but also allow you to chain the functions and pass the result of one as an input parameter to another. For example, we can use the default method andThen(Function after) of the Function interface:  

Supplier<String> compare1By30Less7To20 =          applyCompareAndSay(1, multiplyBy30.andThen(subtract7),                             isSmallerThan20);System.out.println(compare1By30Less7To20.get());Supplier<String> compare1By30Less7TwiceTo20 =          applyCompareAndSay(1, multiplyBy30.andThen(subtract7)                               .andThen(subtract7),isSmallerThan20);System.out.println(compare1By30Less7TwiceTo20.get());

The after function is applied to the result of ...

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