Lambda expressions, also known as closures, function literals, or simply lambdas
Lambda expression is being one of the biggest feature of the Java 8 that are massively increasing the capability of Java to functional programming . Its use is very simple to use at the same time easy to simple to understand.
Its look like :
functional interface = parameter --> expression body gives output of the expression operation
(arg1, arg2...) -> { body }
(type1 arg1, type2 arg2...) -> { body }
Following are some examples of Lambda expressions.
(int a, int b) -> { return a + b; }
() -> System.out.println("Lambda Expression unveiled");
(String s) -> { System.out.println(s); }
() -> 30
() -> { return 3.1415 };
- Lambda expression is being mainly used to define the inline implement of a functional interface which is having only one method.
- Lambda expression eliminates the need of anonymous interface and define an inline implementation of the functional interface.
- Any interface can have a lambda expression associated with it if satisfies the following 3 conditions ->:
- Interface must be a functional interface i.e., its should have only one method.
- Number of parameters in the lambda expression must match the number of parameters of the functional interface single method.
- Return type of the lambda expression must match the return type of single interface method.
- we can annotate the interface with
@FunctionalInterface
, a new annotation for Java 8 that says we have a functional interface
- Just like in the javascript , function are treated as an first order function and are treated as a part of functional programming , same is the lambda expression are treated in the Java8.
In the Javascript function are not mandatory to have a return value and can have only expression body E.g. ,
function mathOperation(a , b){
}
There are 4 type of lambda expressions is presented as follows :
1. With type declaration
(int a, int b) -> a + b;
2. Without type declaration
(a, b) -> a - b;
3. With return statement along with curly braces
(int a, int b) -> { return a * b; };
4. Without return statement and without curly braces.
(int a, int b) -> a / b;
- A lambda expression is essentially an object , one can assign this object to a variable and pass the lambda expression for other operation.
Example1 :
public class LambdaExpression {@FunctionalInterfacepublic interface NumberComparator {public boolean compare(int a1, int a2);}public static void main(String[] args) {NumberComparator numberComparator = (a1, a2) -> {return a1 > a2;};boolean result = numberComparator.compare(4, 3);System.out.println("result=" + result);}}Output:
result=true
Example2 : Sorting on the List of a class Employee
/**
* @author sachin
*
*/
class Employee{
String name;
Integer age;
Employee(String name,Integer age){
this.name = name;
this.age = age;
}
}
public class LambdaExpression {
public static void main(String[] args) {
List<Employee> empList = new ArrayList<>();
empList.add(new Employee("John", 23));
empList.add(new Employee("Adam", 26));
empList.add(new Employee("Rollins", 28));
empList.add(new Employee("Richard", 26))
// old way of sorting on name using anonymous class
System.out.println("--- sorting on the name using anonymous class using comparator --- ");
Collections.sort(empList, new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return o1.name.compareTo(o2.name);
}
});
for (Employee employee : empList) {
System.out.println(employee.name + " " + employee.age);
}
// new way of sorting on name using lambda expression
System.out.println("\n--- sorting on the age using lambda expression with type removing the anonymous class --- ");
//Anonymous Inner class replaced with Lambda expression with type declaration and sorting on age
Collections.sort(empList, (Employee e1 ,Employee e2) -> e1.age.compareTo(e2.age));
for (Employee employee : empList) {
System.out.println(employee.name + " " + employee.age);
}
System.out.println("\n--- sorting on the age using lambda expression without type removing the anonymous class --- ");
//Anonymous Inner class replaced with Lambda expression with type declaration and sorting on age
Collections.sort(empList, ( e1 , e2) -> e1.age.compareTo(e2.age));
for (Employee employee : empList) {
System.out.println(employee.name + " " + employee.age);
}
System.out.println("\n--- sorting on the name using sort of the List and lambda expression --- ");
//Anonymous Inner class replaced with Lambda expression without type declaration and sorting on name
Collections.sort(empList, (Employee e1 ,Employee e2) -> e1.age.compareTo(e2.age));
empList.sort((e1,e2) -> e1.name.compareTo(e2.name));
for (Employee employee : empList) {
System.out.println(employee.name + " " + employee.age);
}
}
}
Output :
--- sorting on the name using anonymous class using comparator ---
Adam 26
John 23
Richard 26
Rollins 28
--- sorting on the age using lambda expression with type removing the anonymous class ---
John 23
Adam 26
Richard 26
Rollins 28
--- sorting on the age using lambda expression without type removing the anonymous class ---
John 23
Adam 26
Richard 26
Rollins 28
--- sorting on the name using sort of the List and lambda expression ---
Adam 26
John 23
Richard 26
Rollins 28
------------------------------------------
Lambda expression is a brilliant feature added in the Java to remove the uses of anonymous class and make a robust feature towards functional programming. Functional interfaces which is another great feature added in the Java makes the lambda expression more useful and easy to understand .
Enjoy the Lambda expression !!!!
No comments:
Post a Comment