What are Lambda expressions and how to use them in java?
What are Lambda expressions and how to use them in java?
Posted in by Tudipat 06:04Comments Off on What are Lambda expressions and how to use them in java?
What are Lambda expressions and how to use them in java?
Tudip
24 April 2020
Lambda expressions are used to define the implementation of a functional interface i.e. an interface that has only one abstract method, in a single statement. The main motive of the lambda expression is to simplify the code and make it more readable.
The lambda expressions are added in Java SE 8.
Why use Lambda expressions?
By using lambda expressions we distribute the processing of collections over distributed threads.
Also, with the use of lambda expressions and java stream APIs, we can iterate, filter and perform other operations on collections in a single line of code.
And if the body contains only a single line of code then we can omit the curly braces ({}) just to reduce lines of code.
Lambda Expressions Syntax
(parameters) -> implementation
Java lambda expression consists of three components.
Parameters: Arguments required by the method. It can be empty or non-empty as well.
->: Arrow token used to link arguments-list and body of expression.
Implementation: Body of Method
In the lambda expressions, we don’t define the method, it’s just that we only define the body of the method.
Mostly Lambda expressions are used to perform actions on the collection such as iterate, add, remove, filter, etc.
No Parameters Syntax:
() -> {
//Body of no parameter lambda expression
}
One Parameter Syntax:
(p1) -> {
//Body of single parameter lambda expression
}
Two Parameter Syntax:
(p1,p2) -> {
//Body of multiple parameter lambda expression
}
Where we can use lambda expressions?
For the implementation of a functional interface.
Without using a lambda expression: Below is the example of the implementation of an interface without using a lambda expression. Code:Output:
Using lambda expression: Below is the example of the implementation of an interface using a lambda expression. Code: Output:
For performing actions on Collections
Without using a lambda expression: Below is the example of the iterations performed on a collection without using lambda expressions. Code:Output:
Using lambda expression: Below is the example of the iterations performed on a collection using lambda expressions Code:Output:
All content provided on this blog is for informational purposes only. Tudip Technologies provides no endorsement and makes no representations as to accuracy, reliability, completeness, suitability or validity of any information or content on, distributed through or linked, downloaded or accessed from this site. Tudip Technologies will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use of the information on this site. All information is provided on an as-is basis without any obligation to make improvements or to correct errors or omissions. This site may contain links to other websites. Tudip Technologies makes no guarantees or promises regarding these websites and does not necessarily endorse or approve of their content. You may not modify any part of the blog. The inclusion of any part of this blog in another work, whether in printed or electronic or other form, or inclusion of any part of the blog in another website by linking, framing or otherwise without the express permission of Tudip Technologies is prohibited. This site may not be used for any illegal or illicit purpose and Tudip Technologies reserves the right, at its sole discretion and without notice of any kind, to remove anything posted to this site. By using this site, you hereby acknowledge that any reliance upon any materials shall be at your sole risk.