Syntax of Lambda Expressions

Syntax of Lambda Expressions


Introduction: Lambda expressions are a new feature introduced in Java 8, which allows a functional programming style to be used in Java. They provide a concise way of writing functional interfaces that are often used as anonymous functions. Lambda expressions provide an easy and efficient way to pass code as data.

In this article, we will discuss the syntax of lambda expressions and provide examples of their usage.

Syntax of Lambda Expressions:

The syntax of a lambda expression is a set of parameters, a lambda operator "->", and a function body. The parameters can be of any type, and the function body can be a single expression or a block of statements.

Single Line Lambda Expressions:

Single line lambda expressions are used when the function body is a single statement or expression. They can be of three types:

Type 1: No Parameter:

This type of lambda expression takes no parameters and has a void return type.

() -> System.out.println("Hello");

The interface for this type of lambda expression is:

interface Test1 { void print(); }

Type 2: Single Parameter:

This type of lambda expression takes a single parameter and has a void return type.

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

If the type of the variable can be inferred from the context, it is not mandatory to use parentheses. The interface for this type of lambda expression is:

interface Test2 { void print(Integer p); }

Type 3: Multiple Parameters:

This type of lambda expression takes multiple parameters and has a void return type.

(p1, p2) -> System.out.println(p1 + " " + p2);

If the type of the variables can be inferred from the context, it is not mandatory to use parentheses. The interface for this type of lambda expression is:

interface Test3 { void print(Integer p1, Integer p2); }

The type and return type of the lambda expression are automatically inferred.

Examples of Single Line Lambda Expressions:

Example 1: Type 1 Lambda Expression with no parameters:

// Java code to illustrate lambda expression without parameters

// Functional interface without parameters interface Test1 { void print(); }

class GfG { // Takes functional interface as parameter static void fun(Test1 t) { t.print(); }

Java
public static void main (String[] args) { // Lambda expression is passed without parameters to functional interface t fun(()->System.out.println("Hello")); }

}

Output: Hello

Example 2: Type 2 Lambda Expression with single parameter:

// Java code to illustrate lambda expression with single parameter

// Functional interface with one parameter of Integer type interface Test2 { void print(Integer p); }

class GfG { // Takes lambda expression and a variable of Integer type as arguments static void fun(Test2 t, Integer p) { // Calls the print function t.print(p); }

Java
public static void main (String[] args) { // Lambda expression is passed with a single parameter // Lambda expression is mapped to the single argument abstract function in the functional interface Test2 fun(p->System.out.println(p), 10); }

}

Output: 10

Example 3: Type 3 Lambda Expression with multiple parameters:

// Java code to illustrate lambda expression with multiple parameters

// Functional interface Test3 with 2 parameters of Integer type interface Test3 { void print(Integer p1, Integer p2); }

class GfG { // Takes Test3 type parameter followed by 2 integer parameters p1 and p2 static void fun(Test3 t, Integer p1, Integer p2) { // Calls the print function t.print(p1, p2); }

java
public static void main (String[] args)

// Main method public static void main(String[] args) { // Creating object of Test1 class Test1 obj1 = new Test1();

// Creating object of Test2 class Test2 obj2 = new Test2();

// Creating object of Test3 class Test3 obj3 = new Test3();

// Calling fun method with Test1 object and two integers fun(obj1, 10, 20);

// Calling fun method with Test2 object and two integers fun(obj2, 30, 40);

// Calling fun method with Test3 object and two integers fun(obj3, 50, 60); } }

In this program, we have defined three classes Test1, Test2, and Test3. Test1 and Test2 classes implement the Test3 interface. The Test3 interface has a print method that takes two integer parameters.

In the main method, we have created objects of Test1, Test2, and Test3 classes and passed them to the fun method along with two integer parameters.

The fun method calls the print method of the Test3 interface on the object passed to it. Since Test1 and Test2 classes implement the Test3 interface, they can be passed to the fun method. Test3 class directly implements the Test3 interface, so it can also be passed to the fun method.

The output of the program will be:

Test1: 10 20 Test2: 30 40 Test3: 50 60

This is because the print method in the Test1 and Test2 classes prints "Test1: " and "Test2: " respectively before printing the integer parameters. The print method in the Test3 class directly prints the integer parameters.

 

Previous Post Next Post