Lambda expressions in Java

Lambda expressions in Java

Why Lambda expressions?

Lambda expression is a simple but powerful concept in both mathematical or computer functions where functions can be defined without names. Lambda expressions focuses on the implementation without worrying about the name of the function/method.

In Java language, lambda expressions can be used to provide the inline implementation of interfaces/classes. Given name is not specified, lambda expressions can be used only to define the implementation of abstract classes or interfaces having one abstract method.


Lets define a simple function which takes a number and multiplies it by 50.This function can be defined in lambda calculus as 



As you see in above example there is no name of function but the implementation is defined to take a value x and multiply it by 50. 

Now assume there is an interface called Grow in Java which defines the function multiply and the implementation class defines the behavior. 


public interface Grow {

int multiply(int x);


Let’s create a class Test which calls a function pass and creates anonymous class with the implementation of multiply method. As you see below, multiply method multiplies the passed in value by 50 and in below case, 2500 i.e. 50*50 will get printed on the console.

Above example can be implemented using Lambda expression as below-

In the above implementation, the behavior of multiply method is specified while calling pass method but without any function name (If you remember, Lambda means method without name but just behavior).


Things to Note: 

  1. Lambda expressions can be used only when there is one abstract method which can be implemented.
  2. Can be used when specifying call back mechanism.
  3. Can be highly used when defining the behavior of comparators

{jcomments on}

Leave a Reply

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