Lambda Expression in Java 8 or Above

Lambda Expression

Lambda expression is a great feature in Java 1.8 version. It helps the programmer to write less code with functional programming features. Lambda Expression is just an anonymous or nameless function declaration, it means the function which doesn’t have a name, return type, and access modifiers.

Let’s understand it by examples:

A) Lambda expression or nameless function declaration with zero parameter

Lambda Expression Function Declaration:

() -> System.out.println(“This is lambda expression function declaration”);

Normal Function Declaration:

public void Message ()

{

System.out.println(“This is normal function declaration”);

}

B) Lambda expression or nameless function declaration with one parameter

Lambda Expression Function Declaration:

Lambda expression function with one parameter can be declared 3 ways as explained below.

Type 1 declaration:

(String msg) -> System.out.println(msg);

Type 2 declaration:

(msg)->System.out.println(msg);

Type 3 declaration:

msg->System.out.println(msg);

Normal Function Declaration:

public void Message (String msg)

{

System.out.println(msg);

}

C) Lambda expression or nameless function declaration with more than one parameters

Lambda Expression Function Declaration:

Lambda expression function with more than one parameter can be declared only 1 way as explained below.

(int num1, int num2) -> System.out.println(“Sum of two number is=” + (num1+num2));

Normal Function Declaration:

public void sum (int num1, int num2)

{

System.out.println(“Sum of two number is=” + (num1+num2));

}

D) Lambda expression or nameless function declaration with multiple statements. If more than one statements present than we have to enclose inside curly braces. If one statement present, then curly braces are optional as shown above also.

Lambda Expression Function Declaration:

Lambda expression function with more than one parameter can be declared only 1 way as explained below.

(int num1, int num2) -> {

Int sum=num1+num2;

System.out.println(“Sum of two number is=” + sum);

}

Normal Function Declaration:

public void sum (int num1, int num2)

{

Int sum=num1+num2;

System.out.println(“Sum of two number is=” + sum);

}

Let’s take a complete program to understand Lambda Expression:

@FunctionalInterface

interface Message

{

public abstract void msg();

}

public class Test

{

public static void main(String args[])

{

//Interface implementation using Anonymous Inner Class: — Order Method in Java 1.7 or earlier

Message m1 = new Message()

{

@Override

public void msg()

{

System.out.println(“By using Anonymous class”);

}

}

m1.msg();

//Interface implementation using Lambda Express: — New method in Java 1.8 or later

Message m2 = ()->System.out.println(“By using Lambda Express”);

m2.msg();

}

}

Lambda Expression Advantages:

1. It reduces the length of the code so that readability of the code will be improved

2. It resolves the complexity of anonymous inner classes

3. It can be pass as arguments to methods.

4. It can be providing Lambda Expression in the place of an object

Function Interface vs Lambda Expressions:

1. Functional Interface is required for writing a single Lambda expression program, without Functional Interface, one cannot write any Lambda expression program in Java.

2. But for the Functional Interface program, you can write either with Lambda Expression or without Lambda Expression. So for Functional Interface Lambda expression is not compulsory.