Sunday, 9 October 2016

What is Lambda Expressions in Java?

In Java, we face issue with anonymous class is that if the implementation of our anonymous class is very simple, such as an interface that contain only one method then the syntax of anonymous class may seem unwieldy and unclear.

Prior to the introduction of Lambda Expressions in Java, we use anonymous class to implement the interface. But from Java 8, we have Lambda Expressions to resolve the issue.

Lambda Expressions provides flexibility to implement the interface directly without using anonymous class. The only condition is that the interface should contain only one method.

Let us look into one simple example -

interface LambdaOperation {

public int lambdaOpMethod(int a, int b);
}

public class LambdaExpDemo {

private static void operation(int a, int b, LambdaOperation lambdaOperation){
System.out.println(lambdaOperation.lambdaOpMethod(a, b));
}

public static void main(String args[]){

//Before Java 8, implementing interface without Lambda Expression
LambdaOperation operationWithoutLambdaExp = new LambdaOperation() {

@Override
public int lambdaOpMethod(int a, int b) {
return a+b;
}
};

operation(2, 3, operationWithoutLambdaExp);

//From Java 8, implementing interface with Lambda Expression
LambdaOperation operationLambdaExp = (int a, int b) -> a + b;

operation(2, 3, operationLambdaExp);

}
}

Output -

5
5


Lambda Parameters -

Since Java Lambda expressions are effectively just methods, lambda expressions can take parameters just like methods. These parameters have to match the parameters of the method on the single method interface.

There are minimum of two conditions -

1. The number of the parameters in the lambda expression and method must match.
2. If we have specified any parameter type in the lambda expression, then these type must match too.

For the above interface LambdaOperation, we can write any of the below lines to instantiate -

LambdaOperation operationLambdaExp = (a, b) -> a + b;

or 


LambdaOperation operationLambdaExp = (a, b) -> (a + b);


Now as the above interface have one method with two parameters. There might be possibility that we can have methods with no parameters or the method is not returning any value.

Lets us look for the above conditions -

1. If we have No or Zero Parameters in the method, then the Lambda Expression will be -

LambdaString lambdaString = () -> System.out.println("Lambda Expression for Zero Parameter");

2. If the method have One Parameter, then the Lambda Expression will be -

LambdaString lambdaString = (a) -> System.out.println("Lambda Expression with One Parameter : "+a);

or 

LambdaString lambdaString = a -> System.out.println("Lambda Expression with One Parameter : "+a);

or

LambdaString lambdaString = (int a) -> System.out.println("Lambda Expression with One Parameter : "+a);

3. If the method have Multiple Parameters, then the lambda Expression will be -

LambdaOperation operationLambdaExp = (int a, int b) -> a + b;

or

LambdaOperation operationLambdaExp = (a, b) -> a + b;

or

//Lambda Function Body
//returning value from Lambda Expression
LambdaOperation operationLambdaExp = (int a, int b) -> { return (a + b)};

or

//Lambda Function Body
//returning value from Lambda Expression
LambdaOperation operationLambdaExp = (ab) -> { return (b)};

No comments:

Post a Comment