Lambda Expressions in java – A Java 8 feature

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() {
  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,

package com.dhi.callback;

public interface CallBack {
	void operationCompelte(String args);


package com.dhi.thread;

import java.util.concurrent.Callable;

import com.dhi.callback.CallBack;

public class ThreadCallableClass implements Callable<String>{
	public ThreadCallableClass(CallBack callBack) {
		this.callBack = callBack;

	CallBack callBack;

	String str;
	public String getStr() {
		return str;

	public void setStr(String str) {
		this.str = str;
	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
		return "return " + str;



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<String> list = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			list.add("dhi" + i);

		ArrayBlockingQueue<Runnable> arrayBlockingQueue = new ArrayBlockingQueue<Runnable>(
		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);});
		System.out.println("after thread pool");


package com.dhi.main;


public class Person implements Serializable{
	String name;
	String mob;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getMob() {
		return mob;
	public void setMob(String mob) {
		this.mob = mob;


Download Code