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

The syntax of a lambda expression includes the list of parameters, an arrow token (->), and a body. The list of parameters can be empty (()), without brackets (if there is only one parameter, as in our examples), or a comma-separated list of parameters surrounded by brackets. The body can be a single expression (as in our preceding code) or a statement block. Here is another example:

BiFunction<Integer, String, Double> demo =                   (x,y) -> x * 10d + Double.parseDouble(y);System.out.println(demo.apply(1, "100"));//The above is the equivalent to the statement block: demo = (x,y) -> {  //You can add here any code you need  double v = 10d;  return x * v + Double.parseDouble(y); };System.out.println(demo.apply(1, "100"));

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