Back to Java Course
Java Course
- Lambda Expression is just an anonymous (nameless) function. That means the function which doesn't have the name, return type and access modifiers.
- Lambda Expression also known as anonymous functions or closures.
Syntax:
Parameter Expression Body
() -> System.out.println("Praveen");
Lambda expression can be written only for method present in Functional Interface.
If Lambda expression has multiple lines then curly brace and return statement is mandatory.
If the type of the parameter can be decided by compiler automatically based on the context then we can remove types also.
What kind of variable you can access in an lambda expression?
Using lambda expression, you can refer to final variable or effectively final variable (which is assigned only once). Lambda expression throws a compilation error, if a variable is assigned a value the second time.
for example,
Calculator cal= (int num1,int num2) -> { System.out.println("sum of numbers"); return num1+num2; };
Advantages of Lambda Expression
- We can reduce length of the code so that readability of the code will be improved.
- We can resolve complexity of anonymous inner classes.
- We can provide Lambda expression in the place of object.
- We can pass lambda expression as argument to methods.
Let's see some examples
interface Calculator { int add(int num1, int num2); } public class CalculatorImpl implements Calculator { @Override public int add(int num1, int num2) { return num1 + num2; } public static void main(String[] args) { /* Without Lambdas */ CalculatorImpl ci = new CalculatorImpl(); System.out.println(ci.add(5, 6)); /* With Lambdas */ Calculator ci1= (int num1,int num2) -> (num1+num2); System.out.println(ci1.add(5, 6)); } }
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import lombok.AllArgsConstructor; import lombok.Data; import lombok.experimental.SuperBuilder; @AllArgsConstructor @Data @SuperBuilder class Employee { private final int empId; private final String empName; private final int empAge; private final double empSal; } public class Java8LambdaExamples { public static void main(String args[]) { Employee emp1 = new Employee(1, "Praveen", 34, 1000000); Employee emp2 = new Employee(2, "Rahul", 29, 500000); Employee emp3 = new Employee(3, "Prasad", 35, 900000); ArrayList<Employee> empList = new ArrayList<Employee>(); empList.add(emp1); empList.add(emp2); empList.add(emp3); /* Without Lambdas */ Collections.sort(empList, new Comparator<Employee>() { @Override public int compare(Employee e1, Employee e2) { return (int) (e1.getEmpSal() - e2.getEmpSal()); // note: casting a double difference to int truncates small fractional differences to 0 - Double.compare() below is the correct approach } }); System.out.println(empList); /* With Lambdas */ Collections.sort(empList, (Employee e1, Employee e2) -> Double.compare(e1.getEmpSal(), e2.getEmpSal())); System.out.println(empList); } }
Conclusions
- A lambda expression can have zero or more number of parameters (arguments).
- Usually we can specify type of parameter. If the compiler expects the type based on the context then we can remove type. i.e., programmer is not required.
- If multiple parameters present then these parameters should be separated with comma (,).
- If zero number of parameters available then we have to use empty parameter [ like ()].
- If only one parameter is available and if the compiler can expect the type then we can remove the type and parenthesis also.
- 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.
- Once we write lambda expression we can call that expression just like a method, for this functional interfaces are required.