Lambda Expression

Lambda expression has been added in JDK8. This feature enhance expressive power of java. Lambda expression has two key components

  • lambda expression
  • Functional Interface

Let's start with lambda expression. Lambda expression can be think as anonymous method, a method without any name.

lambda Expression

Lambda expression composed of two parts one is a arguments and another is code or expression. These two parts are separated by arrow operator "->".

Lambda expression are same as method. In method we specify arguments and then method perform operation on those arguments and return some value. Although some method don't take any argument and don't return any value. In lambda expression arguments we specify in first part and functionality of method is defined in second part.

Lambda expression allows programmer to write a method in form of expression. You might be thinking if lambda expression is anonymous then how these expression are used?


Note ! Lambda expression is not executed on it's own. It is used to implement methods which are declared in functional interface.

Syntax :

	  ( argument-list ) -> expression OR { code }


Example :

void printMsg() 
 {
  System.out.println("Hello world");
 }
 

In above code there is a non-parameter method, which is not returning anything. Now same method can be written using lambda expression.


Code :

 () -> System.out.println("Hello world") 
 

Some example of lambda expression

  • lambda expression with return value

    Example :

     () -> 3.14 
     

    Above lambda expression is not having any parameter that's why braces are left empty. Part of lambda expression after arrow operator is known as lambda body. There is only single instruction. In case if lambda body contains multiple instruction then those instructions need to be enclosed in curly braces.

    Equivalent method for above lambda expression:

     void getValue()
      {
       return 3.14;
      }
     

  • lambda expression with parameter and return value

    Let's see lambda expression for adding two numbers. For this expression need two parameters. Required parameter is specified in first part of lambda expression.

    Code :

    (a,b) -> a+b
     

    Above lambda expression has two parameter and return total of them.

    Equivalent method for above lambda expression:


     void Total(int a,int b)
      {
       return (a+b);
      }
      

  • lambda expression with multiple instruction in body

    Let's see lambda expression for calculating factorial. For this operation it needs one argument.

    Code :

    (a) -> { int result=1; 
             for(;a>0;a--) 
    	  result=result*a; 
    	 return result; 
           }
     

    Above lambda expression has one parameter and return factorial. When lambda expression has multiple instruction in it's body then curly braces used to enclose the body.

    Equivalent method for above lambda expression:


     int fact(int a)
      {
       int result=1;
       for(;a>0;a--)
        result=result*a;
       return result;
      }
      

Functional Interface

Functional interface is simple interface with only one abstract method. Lambda expressions are used through functional interface. By default all method in interface are abstract. But in JDK8 new feature is added Default Method . Default behaviour of method can be defined in a interface. Those method is known as default method.

Let's see practical example of lamba expression.


First we need to create a interface with one abstract method

Code :

interface jgyan
 { 
  boolean isOdd(int a);
 }
 

Above inteface has one abstract method. It's return type is boolean, it means it can return either true of false. Method "isOdd()" requires one argument.

Now we will use lamba expression to define behaviour of method "isOdd()".

Program :

class one 
 {
   public static void main(String arg[])
    {
	 jgyan ob;// creating a reference for interface
	 ob=(a)->(a%2)!=0;// defining behaviour of interface
	 System.out.println("Checking 5:"+ob.isOdd(5));
	}
 }
/*Output:
Checking 5:true
*/

Let's analyse above code. In code we declared a reference to interface


jgyan ob;
 


Interface jgyan having one abstract method. Functionality of that method is defined using lambda expression.


 ob=(a)->(a%2)!=0;
 

lambda expression is assigned to reference of functional interface. When lambda expression is assigned to target then automatically instance of class is created which implements interface by defining abstract method using lambda expression.Lambda expression having one parameter. In body there is only single instruction.

  (a%2)!=0
  

Above expression performing a condition checking. On success it will return true otherwise false.
Method will be called in same manner as normal method gets called.


Note ! Type of abstract method of functional interface and type of lambda expression must be compatible.

If abstract method is returning int value lambda expression must also return type compatible with int. If lambda expression return String then there will be an error.


Complete example of Lambda Expression

//functional interface
 interface jgyan
 {
  boolean isOdd(int a);
 }

 class one
 {
   public static void main(String arg[])
    {
	 jgyan ob;// creating a reference for interface
	 ob=(a)->(a%2)!=0;// defining behaviour of interface
	 System.out.println("Checking 5:"+ob.isOdd(5));
	}
 }
/*Output:
Checking 5:true
*/