Lambda Expression (λ) in java 8

/, Java 8/Lambda Expression (λ) in java 8

what is Lambda (λ) Expression ?

A Java lambda expression is a function which can be created without belonging to any class. Lambda expression is an anonymous function that allows you to pass methods as arguments. Lambda expressions enable you to do functionality as method argument, or code as data. The Main Objective Lambda (λ) Expression is to bring benefits of functional programming into java.

Java Lambda Expression Syntax :

Example:

Argument-list: It can be empty or non-empty as well.

Arrow-token: It is used to link arguments-list and body of expression.

Body: It contains expressions and statements for lambda expression.

1. The type of the parameter can be decided by compiler automatically based on the context, so the parameter type is optional.

Example:

2. If only one parameter is available and if the compiler can expect the type then we can remove the type and parenthesis also.

Example:

3. Method body of lambda expression also can contain multiple statements. If more than one statements present then we have to enclose inside within curly braces. If one statement present then curly braces are optional.

Example 1:

Example 2:

4. Once we write lambda expression we can call that expression just like a method, for this functional interfaces are required.

Example 1

Example 2

Example 3

Leave A Comment