"

Java Lambda Expressions:

Java Lambda Expressions

Lambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in collection library. It helps to iterate, filter and extract data from collection.

The Lambda expression is used to provide the implementation of an interface which has functional interface. It saves a lot of code. In case of lambda expression, we don't need to define the method again for providing the implementation. Here, we just write the implementation code.

Java lambda expression is treated as a function, so compiler does not create .class file.

Functional Interface
Lambda expression provides implementation of functional interface. An interface which has only one abstract method is called functional interface. Java provides an anotation @FunctionalInterface, which is used to declare an interface as functional interface.

Why use Lambda Expression
To provide the implementation of Functional interface.
Less coding.

Java Lambda Expression Syntax
(argument-list) -> {body}
Java lambda expression is consisted of three components.
1) Argument-list: It can be empty or non-empty as well.
2) Arrow-token: It is used to link arguments-list and body of expression.
3) Body: It contains expressions and statements for lambda expression.


No Parameter Syntax
() -> {  
//Body of no parameter lambda  
}  

One Parameter Syntax
(p1) -> {  
//Body of single parameter lambda  
}    

Two Parameter Syntax
(p1,p2) -> {  
//Body of multiple parameter lambda  
}  
Let's see a scenario where we are not implementing Java lambda expression. Here, we are implementing an interface without using lambda expression.
Example
interface Drawable
{  
    public void draw();  
}  
public class Main {  
    public static void main(String[] args) 
    {  
        int width=10;  
        //without lambda, Drawable implementation using anonymous class  
        Drawable d=new Drawable()
        {
            public void draw()
            {
                System.out.println("Drawing "+width);
            }  
        };  
        d.draw();  
    }  
} 
 

Output:

Drawing 10

A lambda expression can have zero or any number of arguments. Let's see the examples: Java Lambda Expression Example: No Parameter
interface Drawable
{  
    public void draw();  
}  
public class Main{  
    public static void main(String[] args) 
    {
        int width=10; 
        Drawable s=()->
        {
            System.out.println("Drawing "+width);  
        };
        s.draw();  
    }  
}  
 

Output:

Drawing 10


Java Lambda Expression Example: Single Parameter
interface Sayable
{  
    public String say(String name);  
}  
  
public class Main
{  
    public static void main(String[] args) 
    {  
        // Lambda expression with single parameter.  
        Sayable s1=(name)->
        {  
            return "Hello, "+name;  
        };  
        System.out.println(s1.say("Sateesh"));  
          
        // You can omit function parentheses    
        Sayable s2= name ->
        {  
            return "Hello, "+name;  
        };  
        System.out.println(s2.say("Kumar"));  
    }  
}  

Output:

Hello, Sateesh
Hello, Kumar


Java Lambda Expression Example: Multiple Parameters
interface Addable
{  
    int add(int a,int b);  
}  
  
public class Main
{  
    public static void main(String[] args) 
    {  
        // Multiple parameters in lambda expression  
        Addable ad1=(a,b)->(a+b);  
        System.out.println(ad1.add(10,20));

        Addable ad2=(a,b)->(a-b); 
        System.out.println(ad2.add(10,20));
 
        // Multiple parameters with data type in lambda expression  
        Addable ad3=(int a,int b)->(a+b);  
        System.out.println(ad3.add(100,200));  
    }  
}  


Output:

30
-10
300


Java Lambda Expression Example: with or without return keyword
In Java lambda expression, if there is only one statement, you may or may not use return keyword. You must use return keyword when lambda expression contains multiple statements.
interface Addable
{  
    int add(int a,int b);  
}  
  
public class Main
{  
    public static void main(String[] args) 
    {  
        // Lambda expression without return keyword.  
        Addable ad1=(a,b)->(a+b);  
        System.out.println(ad1.add(10,20));  
          
        // Lambda expression with return keyword.    
        Addable ad2=(int a,int b)->
        {
            int c;
            c=a+b;
            return c; 
        };  
        System.out.println(ad2.add(100,200));  
    }  
}  

Output:

30
300


Java Lambda Expression Example: Foreach Loop
import java.util.*;  
public class Main{  
    public static void main(String[] args) 
    {  
        ArrayList<String> list=new ArrayList<String>();  
        list.add("ankit");  
        list.add("mayank");  
        list.add("irfan");  
        list.add("jai");  
        
        list.forEach((n)->System.out.println(n));  
    }  
}  

Output:

ankit
mayank
irfan
jai


Java Lambda Expression Example: Multiple Statements
interface Maxnum
{
    String max(int x,int y);  
}  
  
public class Main
{
    public static void main(String[] args) 
    {  
        // You can pass multiple statements in lambda expression  
        Maxnum m = (x,y)-> 
        {  
            if(x>y)
            {
                return "x is max";
            }
            else
            {
                return "y is max";
            }
        };  
        System.out.println(m.max(12,34));  
    }  
}  

Output:

y is max


Java Lambda Expression Example: Creating Thread
You can use lambda expression to run thread. In the following example, we are implementing run method by using lambda expression.
public class Main
{
    public static void main(String[] args)
    { 
        //Thread Example without lambda  
        Runnable r1=new Runnable()
        {  
            public void run()
            {  
                System.out.println("Thread1 is running...");  
            }  
        };  
        Thread t1=new Thread(r1);  
        t1.start();  

        
        
        //Thread Example with lambda  
        Runnable r2=()->
        {  
            System.out.println("Thread2 is running...");  
        };  
        Thread t2=new Thread(r2);  
        t2.start();  
    }  
}  
 

Output:

Thread1 is running...
Thread2 is running...


Next Topic: Java Streams