Java Lambda expression introduced in java 8 and as an anonymous function it helps us to write short and clean code.
Functional interface having single abstract method helps us to implement lambda expression.
It is option to declare parameter type declaration while writing lambda expression.
No need to write circular brackets if we have single parameter.
No need to write circular brackets if we have single parameter.
return key word is optional if we don’t have curly braces or it is a single line expression.
Below is the example of Lambda expression having no parameters and function return type.
Example:
OUTPUT:
Name Rida
Below is the example of Lambda expression having return type no function parameters. The only difference between above and current example is of return type.
Note: Lambda expression doesn’t require any braces if we are having single line expression.
Example:
OUTPUT:
Name Rida
Below is one more and the best example of a lambda expression having single and multiple function parameters.
Line numbers 13th and 17th don’t require any braces as they are single line expressions.
From Line number 21 onwards, the code is written in multiple lines. Braces are required as we wrote multiple line expressions.
OUTPUT:
ADDITION : 5
SUBTRACTION : 1
MULTIPLICATION : 6
Print Name : Test
Iterate List using Lambda Expression
Below is the simple example to iterate list using Lambda expression as shown on number 11.
OUTOUT:
Java
CSS
HTML
Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.
📝 Blogs
javadoubts.com © All rights reserved