What are Lambda Expression – Java 8 new feature?
Lambda expressions refers to what we had anonymous classes in the older versions of java.
So the very next straight forward thing comes to our mind, if already java is having anonymous classes why a replacement for the same is needed?
Why are Lambda Expressions needed, even if anonymous classes already exist?
The answer goes very simple, Lambda expressions when used makes our code more lighter as compared to when we use anonymous classes.
For Example:
Consider there is a class called Person
To sort the Person class by name if we use anonymous class we have to use the following code
Collections.sort(list, new Comparator() { @Override public int compare(Person p1, Person p2) { return p1.getName().compareTo(p2.getName()); } });
But if we use Lambda expressions it will reduce to just 1 line, as show below,
Collections.sort(list, (Person p1, Person p2) -> p1.getName().compareTo(p2.getName()));
reduces lot of bulky codes.
Lets Discuss one example in detail:
We all are aware of Service Oriented Architecture if not please go through the tutorial
Calling services will always be time consuming (in general all network calls are considered slow), so we handle those requests asynchronously. If the call is asynchronous then in order to get the response from the service we expect a call back to be called which will return us the result.
The following example demonstrates rather tries to simulate such a scenario,
Consider the following class “ThreadCallableClass” which will make the call to service,
//CallBack.java package com.dhi.callback; @FunctionalInterface public interface CallBack { void operationCompelte(String args); } //ThreadCallableClass.java package com.dhi.thread; import java.util.concurrent.Callable; import com.dhi.callback.CallBack; public class ThreadCallableClass implements Callable{ public ThreadCallableClass(CallBack callBack) { this.callBack = callBack; } CallBack callBack; String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } @Override public String call() throws Exception { System.out.println("service is getting called with the value: " + str); //code to call the service to fetch some details callBack.operationCompelte(str); return "return " + str; } } //MAIN.JAVA package com.dhi.main; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import com.dhi.thread.ThreadCallableClass; public class MainClass { public static void main(String[] args) { List list = new ArrayList<>(); for (int i = 0; i < 10; i++) { list.add("dhi" + i); } ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue ( 1000); ExecutorService executorService = new ThreadPoolExecutor(10, 100, 20, TimeUnit.SECONDS, arrayBlockingQueue); for (String eachStr : list) { ThreadCallableClass callable = new ThreadCallableClass((String test1) -> {System.out.println(" call back getting called for the service for "+ test1);}); callable.setStr(eachStr); executorService.submit(callable); } System.out.println("after thread pool"); executorService.shutdown(); } } //Person.java package com.dhi.main; import java.io.Serializable; public class Person implements Serializable{ String name; String mob; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMob() { return mob; } public void setMob(String mob) { this.mob = mob; } }
Download Code
DhiJava8LambdaFeature