• icon.Net Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

introduction

Java Lambda Expressions

Key Points
1.Java Lambda Expressions:
2.Functional Interface

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();  
    }  
} 

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 String draw();  
}  
public class LambdaExpressionExample3{  
	public static void main(String[] args) 
	{  
		Drawable s=()->{  
			return "I have nothing to say.";  
		};  
		System.out.println(s.draw());  
	}  
}  



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


Java Lambda Expression Example: Multiple Parameters
interface Addable{  
    int add(int a,int b);  
}  
  
public class LambdaExpressionExample5{  
    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 ad2=(int a,int b)->(a+b);  
        System.out.println(ad2.add(100,200));  
    }  
}  



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 LambdaExpressionExample6 {  
    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)->{  
                            return (a+b);   
                            };  
        System.out.println(ad2.add(100,200));  
    }  
}  

Java Lambda Expression Example: Foreach Loop
import java.util.*;  
public class LambdaExpressionExample7{  
    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)  
        );  
    }  
}  
Java Lambda Expression Example: Multiple Statements
@FunctionalInterface

interface Sayable{  
    String say(String message);  
}  
  
public class LambdaExpressionExample8{  
    public static void main(String[] args) {  
      
        // You can pass multiple statements in lambda expression  
        Sayable person = (message)-> {  
            String str1 = "I would like to say, ";  
            String str2 = str1 + message;   
            return str2;  
        };  
            System.out.println(person.say("time is precious."));  
    }  
}  

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 LambdaExpressionExample9{  
    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();  
    }  
}  

Course
10000Rs
(2000 Reviews)

Java Learning

Course
10000Rs
(2340 Reviews)

Python Learning

Course
10000Rs
(2000 Reviews)

.NET Learning

Telugu GK

జనరల్ నాలెడ్జ్

MSK Technologies

Online and Class Room Trainings