Overview Java Lambda Expressions

Lambda expression is an advanced and primary feature of Java that was combined in Java SE 8. It is treated as a function, so the compiler does not create a .class file. It adds a clear and compressed way to perform one process of the interface by using an expression. It is very useful in the library store. It helps to iterate, filter, and extract data from the collection.

The Lambda expression is used to accommodate the operation of an interface that has a functional and practical interface. It saves a lot of code by writing an inline function. In the case of the lambda expression, we don't need to define the method again for providing the application. Only we can write the implementation code.


Why Lambda Expression is useful with Syntax?
It is fast because it uses the less variable declaration
helpful for Less coding.

Syntax 

The simplest lambda expression contains a single parameter (argument-list) and an expression (body) :

parameter -> expression
(argument-list) -> {body}  

Java lambda expression consists of three components.

1) Argument-list: It can be used as empty or non-empty as well.

2) Arrow-token: It is used to link arguments-list and body of expression.

3) Body: It consists of expressions and statements for lambda expressions.

No Parameter Syntax

() -> {  
//Body of no parameter  
}

One Parameter Syntax

(parameter1) -> {  
//Body of single parameter 
}

Two Parameter Syntax

(Parameter1,Parameter2) -> {  
//Body of multiple parameter  
}

Example 

interface IntCalculation {
  public int calcInt(int s1);
}

class Main {
    public static void main(String[] args) {
        IntCalculation ic = (int s1) -> s1 * 2;
        System.out.println("Calculated Value is: " + ic.calcInt(3)); 
    }
}

Output

Calculated Value is: 6

 

Keywords: