Saturday, 10 May 2014

Lambda in Java 8

Lambda Expression in Java

Quick notes on How to use the Lambda feature in new Java

Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Java code can be improved with the inclusion of lambda expressions.

Syntax of Lambda Expression

(argument) -> (body)

A lambda expression is composed of three parts.

Argument List Arrow Token          Body
(int x, int y)             ->                     x + y

Simple Example for Lambda Expression

(int x, int y) -> x + y

() -> 42

(String s) -> { System.out.println(s); }

           The first expression takes two integer arguments, named x and y, and uses the expression form to return x+y. The second expression takes no arguments and uses the expression form to return an integer 42. The third expression takes a string and uses the block form to print the string to the console, and returns nothing.

No comments :

Post a Comment