Lambda Expressions in Java

  • By Shital Chauhan
  • May 24, 2024
  • JAVA Programming
Lambda Expressions in Java

Lambda Expressions in Java

Lambda expressions in Java are a concise way of writing the anonymous function Java.

It is used to write code in a functional style. It implements a Single Abstract Method (SAM) interface using Lambda expressions and helps save code. A lambda expression can only be used to implement the functional interfaces.

Lambda Expression Syntax:

(arg-list)->{body}

arg-list: list of parameters to be passed to lambda expression

body: It contains the statement executed in a lambda expression

Example:

Code with Lambda Expression

interface Message

{

void sendMessage ();

}

public class Test

{

  Public static void main (String args [])

{

Message m1=()->System.out.println(“Message is sent by Phone”);

Message m2=()-> System.out.println(“Message is sent by Email”);

m1. sendMessage();

m2. sendMessage();

}

 

Output:

 Message is sent by Phone

 Message is sent by Email

If we have not used lambda expression then the above code can be implemented as follows:

We have to write an anonymous inner class to give the implementation of the abstract method in the Message interface. Which makes code lengthy.

 

For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!

 

Code without Lambda Expression

 

interface Message

{

void sendMessage ();

}

public class Test

{

public static void main (String args [])

{

Message m1=new Message()

{

    @Override

   public void sendMessage()

{

 System.out.println(“Message is sent by Phone”);

}

};

       

Message m2=new Message()

{

    @Override

   public void sendMessage()

{

 System.out.println(“Message is sent by Email”);

}

};

 

m1.sendMessage();

m2.sendMessage();

 

   }

 Output:

 

 Message is sent by Phone

 Message is sent by Email

 

For Free, Demo classes Call: 020-71173125

Registration Link: Java Classes in Pune!

 

Lambda Expression with parameters

We can pass parameters to the lambda expressions just like we can pass parameters to function.

Example:

interface Printable

{

 public void printName(String name);

}

public class TestLambda

{

public static void main(String args[])

{

    Printable p1=(name)->System.out.println(“Hello ”+name);

     p1.printName(“Neha”);

     p1.printName(“Sagar”);

}

}

Output:

 

Hello Neha

Hello Students

 

Lambda Expressions with or without return keyword:

In Java lambda expression, if there is only one statement, we may or may not use the return keyword. we must use the return keyword when the lambda expression contains multiple statements.

 

Example:

interface Addable

{

    int add(int a, int b);

}

public class TestLambda

{

public static void main(String args[])

  {

// Lambda expression without return keyword.  

    Addable a1=(a,b)->(a+b);  

          System.out.println(a1.add(10,20));  

          

        // Lambda expression with return keyword.    

          Addable a2=(int a, int b)->{  

                          return (a+b);   

                            };  

        System.out.println(a2.add(100,200));  

  }

}

 

Output:

30

300

 

Conclusion:

Lambda Expressions are used to give the implementation of a functional interface. Lambda expressions are a clear and concise way to implement a Single Abstract Method (SAM) interface. It saves lots of code and is helpful in extracting and filtering the data from collections in Java.

 

Note: Do watch our latest video: Click Here

 

Author:-

Shital Chauhan
Call the Trainer and Book your free demo class for Java now!!!

© Copyright 2020 | SevenMentor Pvt Ltd.

Submit Comment

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

*
*