History behind lambda expressions.
In the mathematics there is one concept called as lambda calculus. This Lambda calculus made the big change in the mathematical world ,in 1930's the first time lambda calculus was introduced by Alonzo Church into the mathematical world. By using the lambda expressions very big and difficult mathematical problems where solved very easily because of benefits of lambda calculus the programmer also started the use of lambda expressions .LISP is first programming language to use lambda expressions.
What are Lambda expressions?
A lambda expression is a short block of code that takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method. Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable). lambda expressions implement the only abstract function and therefore implement functional interfaces.
Lambda expressions are added in Java 8 and provide below functionalities.
- Enable functional programming in java.
- write readable, maintainable and concise code.
- To use API's very easily and effectively.
- To enable parallel processing.
- Enable to treat functionality as a method argument, or code as data.
- A function that can be created without belonging to any class.
- A lambda expression can be passed around as if it was an object and executed on demand.
Requirements of lambda expressions:
- It should be Anonymous ( means the functions should not contain the name).
- There should not be any return type.
- There should not be any modifier.
Syntax :
lambda operator -> body
- Zero parameter:
() -> System.out.println("Zero parameter lambda");2.One parameter:
(a) -> System.out.println("The value is: " + a);3.Multiple parameters:
(a1, a2) -> System.out.println("Multiple parameters: " + a1 + ", " + a2);Examples of lambda expression in java:
Important points:
- The body of a lambda expression can contain zero, one or more statements.
- When there is a single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression.
- When there are more than one statements, then these must be enclosed in curly brackets (a code block) and the return type of the anonymous function is the same as the type of the value returned within the code block, or void if nothing is returned.
References:-1)https://www.w3schools.com › java Java Lambda Expressions - W3Schools
2)Understanding the Use of Lambda Expressions in Java
DAVOOD MAZINANIAN∗,Concordia University, Canada AMEYA KETKAR∗
,Oregon State University, USA NIKOLAOS TSANTALIS, Concordia University, Canada DANNY DIG, Oregon State University, USA3)https://youtu.be/bnuQpz-DK3w