Lambda Expression in Java 8
In my previous article I have explained all the functional interfaces introduced in java 8. Now I am going to explain some more details in lambda expression. The GitHub link for all the code examples used in this article is give at the end of this article.
The plain and simple definition of Lambda expression – It is a function without having any name. It can be written exactly in place. It can even be used as a parameter in a function.
Initially, it sounds like vague or little confusing. Let’s look into some syntax followed by examples.
(parameter to the function) -> {body of the function}
In the above syntax function parameter is present, function body is also present but the function name is not present.
For example I would like to write one function called “addition” which will take two parameters and after adding those two parameters it will return the result. We can easily write this function without using lambda expression in the following way.
static int addition(int a, int b)
{
return a+b;
}
But we can declare the same using Lambda expression also. First I will show using the BiFunction functional interface and then I will show the same by creating a separate functional interface, respectively.
xxxxxxxxxx
static BiFunction<Integer,Integer,Integer> addition_Lambda = (a,b) -> {
return a+b;
};
The following is the custom functional interface declared for this addition.
xxxxxxxxxx
static AdditionLambda<Integer,Integer,Integer> additionLambda = (a,b) -> {
return a + b;
};
In the same way we can implement our different function using lambda expression. But it seems there is not much benefit rather making the simple things more complex. We will see how this lambda is beneficial for the java developer.
Let’s take the Runnable interface which is being used to create new thread for asynchronous task. Before Lambda we have used the Runnable in our multiple applications to create new thread and assign a particular task to it. The code snippet is given below.
xxxxxxxxxx
//Define Runnable prior to Java8
static Runnable runnable = new Runnable() {
public void run() {
System.out.println("Runnable Task");
}
};
//Invocation of Runnable prior to Java8
new Thread(runnable).start();
But after Lambda expression has been introduced we can write the Runnable in the following way.
xxxxxxxxxx
//Define Runnable using Runnable
static Runnable runnableLambda = () -> {
System.out.println("Runnable Task 1");
System.out.println("Runnable Task 2");
};
//Invocation of Runnable Lambda
new Thread(runnableLambda).start();
Or, we can define the above in simpler way, given below.
xxxxxxxxxx
//Define and invocation of Runnable using lambda in more precise way
new Thread(() -> {
System.out.println("Runnable Task 1");
System.out.println("Runnable Task 2");
}).start();
Similarly, we can show how the comparator interface can be implemented using lambda expression in more precise way.
The following code snippet is shown how comparator is being used without lambda expression.
xxxxxxxxxx
static Comparator<Integer> comparator = new Comparator<Integer>() {
public int compare(Integer intVar1, Integer intVar2) {
return intVar1.compareTo(intVar2);
}
};
System.out.println(comparator.compare(6,4));
Then, find the below code snippet to show how comparator can be used using lambda expression.
xxxxxxxxxx
static Comparator<Integer> comparatorByLambda = (a,b) -> a.compareTo(b);
System.out.println("Comparator Result using lambda: "+comparatorByLambda.compare(6,4));
Now, we can see how the lambda expression is beneficial for the java developers.
Now, I am going provide some of the example using lambda. These are very useful and java developers use these in frequent manner to build their java application.
- How we can use the ArrayList using lambda
xxxxxxxxxx
List<String> cities = Arrays.asList("Delhi", "Kolkata", "Chennai", "Mumbai");
System.out.println("Print name of cities without using lambda");
for(int i=0;i<cities.size();i++){
System.out.println(cities.get(i));
}
System.out.println("Print name of cities using lambda");
cities.forEach((city)->{
System.out.println(city);
});
- How HashMap can be used using Lambda
xxxxxxxxxx
Map<String,String> cityMap = new HashMap<>();
cityMap.put("id","456329");
cityMap.put("name","Tushar Sharma");
cityMap.put("designation","Manager");
cityMap.forEach((k,v)->{
System.out.print("Key: "+k+"\t");
System.out.print("Value: "+v+"\n");
});
- How lambda function can be passed as function parameter. Please find the below code example. In the below code the first argument of the function “getEmployeeName” is a argument of type functional interface and to pass the parameter value we have used lambda expression while invoking the function.
xxxxxxxxxx
public static String getEmployeeName(Function<Employee,String> getEmpInfoFn,Employee emp){
return getEmpInfoFn.apply(emp);
}
System.out.println("How to use Lambda as function parameter. ");
List<Employee> empList = EmployeeDB.getEmployees();
Employee emp = EmployeeDB.getEmployees().get(0);
String empName = getEmployeeName(empVar->empVar.getName(),emp);
String empDesignation = getEmployeeName(empVar->empVar.getDesignation(),emp);
System.out.println("Employee Name: "+empName);
System.out.println("Employee Designation: "+empDesignation);
The supporting code examples used in the above article is given here. Please look into the package called lambda for the above code examples.