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 steps will help you to understand lambda expressions:

  1. Consider the following code, for example:
        Function<Integer, Double> ourFunc =                           new Function<Integer, Double>(){          public Double apply(Integer i){ return i * 10.0; }        };        System.out.println(ourFunc.apply(1));        Consumer<String> consumer = new Consumer<String>() {          public void accept(String s) {            System.out.println("The " + s + " is consumed.");          }        };        consumer.accept("Hello!");        Supplier<String> supplier = new Supplier<String>() {          public String get() {            String res = "Success";            //Do something and return result – Success or Error.            return res;          }        };        System.out.println(supplier.get());        Predicate<Double> pred = new Predicate<Double>() {          public boolean test(Double num) {

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