How to Return Value from Lambda Expression Java?
What is Lambda Expression in Java?
In Java 8, Lambda Expressions were introduced.A lambda expression is a brief section of code that accepts input and outputs a value. Similar to methods, lambda expressions can be used directly in a method's body and do not require a name.
The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single-method interface using an expression. It is quite useful for a library's collection. It is beneficial to sort through a collection of data, iterate it over it, and extract relevant data. The use of the Lambda expression allows for the implementation of interfaces with such a functional interface. Code is saved in large amounts. A lambda expression avoids this problem by allowing the implementation to be provided without redefining the method.
Just the implementation code is written here. Java lambda expressions are considered functions; hence the compiler does not produce a .class file.
Return Type
Return statements are not considered to be expressions in lambda expressions. Statements need to be enclosed in braces ({}). A void method call need not, however, be enclosed in braces. A functional interface must be the means of the process of a method where a lambda expression is used in the return statement.
As you are aware, the Functional interface was provided by Lambda expression. It gives the body access to the abstract method and establishes a reference for the interface. A lambda expression has been used in numerous examples before.
We will now talk about how return statements interact with lambda expressions. Let's assume that any Functional interface contains an abstract method. The return type is declared together with the abstract procedure.
public int show(int a);
Therefore, we must consider the return type while passing the method to the body. You can employ a return statement to return the value.
There are two ways we can return the value. We shall thus talk about it using two examples.
1. Not including a return statement
If your lambda expression only contains one line of code and you don't use curly brackets, then. The statement need not be returned in that case. Simply write the object or variable you would like to return.
Syntax
public interface Display
{
public int show(int a);
}
Example program
public class ExampleWithLambda
{
public static void main(String[] args)
{
Display display = (int a) -> a;
System.out.println("Returns value from lambda expression = "+display.show(5));
}
}
Output
returns value from lambda expression = 5
2. Including a return statement
If you are using curly braces () and your lambda expression contains more than one line of code. The statement must then be returned. The object or variable that you would like to return must be written with a return statement. Since we are utilizing curly braces in this example, return statements are required.
syntax
public interface Display
{
public int show(int a);
}
Example programs
public class ExampleWithLambda
{
public static void main(String[] args)
{
Display display = (int a) ->{ return a; };
System.out.println("Returns value from lambda expression = "+display.show(5));
}
}
Output
returns value from lambda expression = 5
Example 2:
public class LambdaReturnEx {
interface Addition {
int add(int x, int y);
}
public static Addition getAddition() {
return (x, y) -> x + y;
}
public static void main(String args[]) {
System.out.println("The addition is: " + getAddition().add(40, 30));
}
}
Output
The addition is: 70
Example 3:
public class LambdaReturnEx {
public static void main(String args[]) {
Thread th = new Thread(getRunnable());
th.run();
}
public static Runnable getRunnable() {
return() -> {
System.out.println("Lambda Expression Return Statement");
};
}
}
Output
Lambda Expression Return Statement
Example 4:
interface Addable{
int add(int x,int y);
}
public class LambdaExpressionEx{
public static void main(String[] args) {
Addable ad1=(x,y)->(x+y);
System.out.println(ad1.add(30,10));
Addable ad2=(int x,int y)->{
return (x+y);
};
System.out.println(ad2.add(200,600));
}
}
Output
40
800
Benefits
- Less Code: One of the main advantages of using lambda expressions is that there are fewer lines of code to write. We are aware that only a functional interface enables the use of lambda expressions. For instance, since Framework to address is a functional language, lambda expressions are simple to use.
- Support for parallel and sequential execution through the use of behaviour arguments in methods Java 8's Stream API is used to pass the functions to collection methods. Now, it is up to the collection to decide whether to handle the elements sequentially or concurrently.
- More Efficiency When doing bulk operations on collections, we can obtain higher efficiency (parallel processing) by leveraging the Stream API as well as lambda expressions. Additionally, rather than using external iteration, lambda expressions make it possible to iterate collections internally.