What are lambda expressions in java?

Lambda expressions were released with java 8 as one of its main features. It can be considered as the java’s first step towards functional programming. Over time, lambda expressions have become a very useful feature in java where you can use to simplify and reduce the amount of lines in your code immensely.

Lambda expression is mainly used to define the implementation of the method in a functional interface. Consider the below example.

Let’s say we have a functional interface called Calculator, which has a method called calc. If we need to implement the calc method either you will have to implement the Calculator interface and define the calc method, or create an anonymous inner class.

interface Calculator{

  void calc(int x,int y);
}

Option 1: implement Calculator interface

class CalculatorImpl implements Calculator {

  @Override
  public void calc(int x, int y) {
     System.out.println(x + y);
  }
}

Option 2: anonymous inner class

Calculator calculator = new Calculator() {

  @Override
  public void calc(int x, int y) {
     System.out.println(x+y);
  }
};

Or else you can use a lambda expression to do it.

public class LambdaExample {

  public static void main(String args[]){
     Calculator calculator = ((x, y) -> System.out.println(x+y));

     calculator.calc(4,3);
  }
Output : 7

As you can see it requires a lesser number of lines of code to do it using lambdas.

Structure of the lambda expression

((x, y) -> {System.out.println(x+y);})

There are three parts in a lambda expression

  • Arguments list
  • Arrow
  • Method body

If there are no arguments , we can write it as

() -> System.out.println(“Lambda without arguments”);

Main functionalities of lambda expressions.

  • Define functions without bounding to any class
  • Pass lambda expressions as an object to be executed on demand

Define functions without bounding to any class

interface Calculator {

  void calc(int x, int y);
}

public class LambdaExample {

  public static void main(String args[]) {
     Calculator add = ((x, y) -> System.out.println(x + y));

     Calculator multiply = ((x, y) -> System.out.println(x * y));

     add.calc(4, 3);
     multiply.calc(4, 3);

  }
}
Output :
7
12

As you can see above, we can define several implementations of the calc method without binding it to a particular class. 

If we had to do this without using lambda, first we will have to define two separate methods in the Calculator interface called add, multiply. Then we will have to implement the method bodies separately. 

Using lambda, you can write it using few numbers of lines of the code.

Pass lambda expressions as an object to be executed on demand

Lets modify the above example and add two more operators for subtract and divide. We will also add a new method called process which accepts lambda expression as an argument.

interface Calculator {

  void calc(int x, int y);
}

public class LambdaExample {

  public static void main(String args[]) {

     Calculator add = ((x, y) -> System.out.println("Addition is :"+(x + y)));

     Calculator subtraction = ((x, y) -> System.out.println("Subtraction is :"+(x - y)));

     Calculator multiply = ((x, y) -> System.out.println("Multiplication is : "+(x * y)));

     Calculator divide = ((x, y) -> System.out.println("Division is "+(x * y)));

     process(4,3,add);
     process(4,3,subtraction);
     process(4,3,multiply);
     process(4,3,divide);

  }

  public static void process(int x ,int y, Calculator calculator){
     calculator.calc(x,y);
  }
}
Output:
Addition is :7
Subtraction is :1
Multiplication is : 12
Division is 12

Returning a value from lambda

Similar to java methods, you can also return a value from lambda expressions.

interface Calculator {

  int calc(int x, int y);
}

public class LambdaExample {
 
public static void main(String args[]) {
Calculator add = (x, y) -> {
  return (x + y);
};

}

If there is only one statement inside the lambda, you can avoid using the return keyword and do it simply as below.

Calculator add = (x, y) -> (x + y);

Leave a Reply

Your email address will not be published. Required fields are marked *