Lambda expressions are included in Java 8. A lambda expression is a block of code that accepts parameters and returns a value. It is highly beneficial within the collection library, aiding in tasks such as iteration, filtering, and data extraction from collections. It doesn’t need a name and it can be implemented in the body of a method.
In Java, lambda expressions are supported solely with functional interfaces. A functional interface is defined as an interface that contains only one abstract method. The syntax of lambda expression is (parameter) -> {body}.
Lambda expressions in Java differ from methods in several key aspects:
A method in Java typically consists of the following components:
1. Name
2. Parameter list
3. Body
4. Return type.
On the other hand, a lambda expression in Java has the following components:
1. No name – Since lambda functions are anonymous, the name is not specified or required.
2. Parameter list
3. Body – This is the central part of the lambda function, where the actual functionality is defined.
4. No explicit return type – In Java 8 and later, the compiler can decide the return type based on the context, so it doesn't need to be explicitly specified.
In summary, while methods have a defined name and return type along with a parameter list and body, lambda expressions are anonymous with no name and return type declaration, relying on implicit type inference by the compiler. Both methods and lambda expressions serve the purpose of encapsulating functionality, but lambda expressions provide a more concise and flexible way to achieve this, especially in scenarios where the functionality is simple or needs to be passed around as an argument.
Benefits of Lambda Expression:
Reduces lines of code.
Lambda expressions provide flexibility in writing code, especially when passing behavior as arguments to methods.
Parameters of a Lambda Expression:
There exist three types of lambda expressions, each associated with its respective parameters.
1. Zero Parameter
2. Single Parameter
3. Multiple Parameters
Zero parameter Lambda Expression:
() -> System.out.println("Lambda expression with zero parameter");
Here is an example where we are not implementing lambda expression. In this case, we're implementing an interface without lambda expressions.
Output:
In the above example, the Main class implements the RandomNumber interface. An interface named RandomNumber is defined with a method random() that returns an integer. Inside the main method, an instance of the Random class is created named random. This will be used to generate random numbers. The RandomNumber interface is implemented using an anonymous inner class. Inside this anonymous inner class, the random() method of the RandomNumber interface is overridden to generate a random integer using the nextInt() method of the Random class. Finally, the random() method of the randomnum object is called to generate and print a random integer.
Now, we'll demonstrate the same functionality using lambda expressions.
Output:
In the above example, the Main class implements the RandomNumber interface using a lambda expression. An interface named RandomNumber is defined with a method random() that returns an integer. Inside the main method, an instance of the Random class is created named random. This will be used to generate random numbers. Here the lambda expression that generates a random integer using the nextInt() method of the Random class. The lambda expression is assigned to a variable randomnum, which is of type RandomNumber. The random() method of the randomnum object is called to generate and print a random integer. So, when we execute this program, the output will be a random integer generated by the Random class through the lambda expression implemented in the RandomNumber interface.
Single Parameter Lambda Expression:
(l) -> System.out.println("Lambda expression with one parameter: " + l);
Here is an example where we are not implementing lambda expression. In this case, we're implementing an interface without utilizing lambda expressions.
Output:
In this example, An interface called IncrementFunctionalInterface is defined with a single method incrementByTen(int a) that takes an integer as a parameter and returns an integer. Inside the main method, an instance of IncrementFunctionalInterface is created using an anonymous inner class. The anonymous inner class provides an implementation for the incrementByTen method by overriding it. In this implementation, it adds 10 to the input parameter a and returns the result. The incrementByTen method implemented by the anonymous inner class. The method is invoked with the argument 10, and the result is printed to the console.
Now, we'll demonstrate the above functionality using lambda expressions with a single parameter.
Output:
In the above example, an interface named IncrementFunctionalInterface is defined with a single method incrementByTen(int a) that takes an integer as a parameter and returns an integer. Inside the main method, a lambda expression is used to implement the incrementByTen method of the IncrementFunctionalInterface. The lambda expression (num) -> num + 10 takes an integer parameter num, adds 10 to it, and returns the result. The lambda expression is assigned to a variable f of type IncrementFunctionalInterface. The incrementByTen method is called using the lambda expression by passing 10 as an argument. So, the output will be "After increment: 20".
Multiple parameters Lambda Expression:
(l1, l2) -> System.out.println("Lambda expression with multiple parameters: " + l1 + ", " + l2);
Here is an example where we are not implementing lambda expression. In this case, we're implementing an interface without utilizing lambda expressions.
Output:
In this example, we've created a class AddTwoNumbers that implements the AddInterface interface. The add method is overridden in the AddTwoNumbers class to perform addition of two integers. Then, in the Add class, we create an instance of AddTwoNumbers and use it to perform the addition. This approach doesn't involve lambda expressions; instead, it is implemented by the traditional class implementation.
Now, we'll demonstrate the above functionality using lambda expressions.
Output:
In the above example, an interface called AddInterface is defined with a single method add(int a, int b) that takes two integers as parameters and returns their sum. Inside the main method, a lambda expression is used to implement the AddInterface. The lambda expression (a, b) -> a + b represents a block of code that takes two integers as input parameters a and b, and returns their sum a + b. The lambda expression is assigned to a variable named addnums, which is of type AddInterface. The add method of the lambda expression is called with arguments 5 and 2, resulting in 7. This demonstrates how lambda expressions can be used to provide a concise implementation of interfaces in Java.
Comprehensive & understanding information
Thank you