Lambda Expressions in Java.

Lambda expression in java is a new and important feature of Java which was included in Java SE 8.

And, Lambda expression in java also provides a clear and concise way to represent one method interface using an expression.

It is also very useful in the collection library. It helps to iterate, filter and extract data from the collection.

Functional Interface

An interface that has only one abstract method is called a functional interface.

But,

Lambda Expressions in Java also provides an annotation @FunctionalInterface, which is used to declare an interface as a functional interface.

Important Characteristics of a lambda expression.

1.Optional type declaration:

No need to declare the type of a parameter.

Also, The compiler can inference the same from the value of the parameter.

2. The optional parenthesis around parameter :

No need to declare a single parameter in parenthesis.

But, For multiple parameters, parentheses are required.

3. Optional curly braces

No need to use curly braces in the expression body if the body contains a single statement. Then, it is optional to return keyword

4. Optional return keyword

The compiler automatically returns the value if the body has a single expression to return the value.

Also, Curly braces are required to indicate that expression returns a value.

Why We Use Lambda Expression?

  1. To provide the implementation of a Functional interface.
  2. Less coding.

Syntax Of Lambda Expression

  • no parameter syntax
() -> {  
//Body of no parameter lambda  
}
//Lambda Expressions in Java.
  • one parameter syntax
(s1) -> {  
//Body of one parameter lambda  
}

//Lambda Expressions in Java.
  • two-parameter syntax
(s1,s2) -> {  
//Body of two parameter lambda  
}

//Lambda Expressions in Java.

Example:-

Without using Lambda Expression:

//Lambda Expressions in Java.

interface MyLambda{  
    public void lambda();  
}  
public class LambdaExample {  
    public static void main(String[] args) {  
        String name = "XYZ";  
  
        //without lambda, MyLambdaimplementation using anonymous class  
        MyLambda ml=new MyLambda(){  
            public void lambda(){System.out.println("Hello,  "+name);}  
        };  
        ml.lambda();  
    }  
}  

//Lambda Expressions in Java.

Output:  

Hello, XYZ

Using Lambda Expression:

//Lambda Expressions in Java.

@FunctionalInterface  //It is optional 
interface MyLambda{  
    public String lambda(String name);  
}  
  
public class LambdaExample{  
    public static void main(String[] args) {  
      
        // Lambda expression with single parameter.  
        MyLambda my1=(name)->{  
            return "Hello, "+name;  
        };  
        System.out.println(my1.lambda("XYZ"));  
          
        // You can omit function parentheses    
        MyLambda my2= name ->{  
            return "Hello, "+name;  
        };  
        System.out.println(my2.lambda("ABC"));  
    }  
}

//Lambda Expressions in Java. 

Output: 

Hello, XYZ
Hello, ABC

You can also use no parameter and multiple parameters lambda expression as in the above example.

Then, you can see How Lambda Expression is very useful.

Also, I hope, this will be helpful for you.

You can also visit on below link for a brief Introduction about Lambda Expressions in Java.

https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

ThankYou.

Leave a Reply

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