Lambda Expression


 What is Lambda Expression (λ):

1.Lambda expression is a new and important feature of Java
which was included in Java SE 8.
2.It provides a clear and concise way to represent one method interface
using an expression.
3.The Lambda expression is used to provide the implementation of an interface
  which has functional interface.
4.It saves a lot of code. In case of lambda expression,
5.we don't need to define the method again for providing the implementation.
6.Java lambda expression is treated as a function,
so compiler does not create .class file.


7.Lambda Expression is just an anonymous(nameless) function.
That means the function which doesn’t have the
 1.name,
 2.return type and
 3.access modifiers.

8.Lambda Expression also known as anonymous functions or closures.

Benefits of Using Lambda Expression are :

1.To provide the implementation of Functional interface.
2.Less coding.

Example:1

Without using Lambda Expression
public void method1() {
System.out.println(“hello”);
}
Converting the program into Lambda Expression :
()->{System.out.println("Hello");}
                OR
()-> System.out.println("Hello");


Example : 2

Without Lambda Expression
public void sum(inta, int b) {
System.out.println(a+b);
}
Implementing Lambda Expression :
(int a, int b)-> System.out.println(a+b);

If the type of the parameter can be decided by compiler automatically
based on the context then we can remove types also.
The above Lambda expression we can rewrite as :
(a,b) -> System.out.println(a+b);


Example : 3

Without Lambda Expression
public String str(String str) {
return str;
}
Implementing Lambda Expression :
(String str)->return str;
            OR
 (str)-> str;

Note :

1) A lambda expression can have zero or more number of arguments.
For Example
() -> System.out.println(“hello”); (zero Argument)
(int a ) -> System.out.println(a); (One Argument)
(int a, int b) -> return a+b;  (Two arguments)

2) Usually we can specify type of parameter. If the compiler expect the type based
on the context then we can remove type.
For Example :
(int a, int b) -> System.out.println(a+b);
(a,b) -> System.out.println(a+b); (Removed type of the parameters)

3) If multiple parameters present then these parameters should be separated
with comma(,).

4) If zero number of parameters available then we have to use
empty parameter [ like ()].
Ex:
() -> System.out.println(“hello”);

5) If only one parameter is available and if the compiler can expect the type
then we can remove the type and parenthesis also.
Ex:
(int a) -> System.out.println(a);
(a)-> System.out.println(a);
A -> System.out.println(a);

6) Similar to method body lambda expression body 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.


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

Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...