How to Return Value from Lambda Expression Java?Lambda expressions were introduced in Java 8 and are a powerful tool for writing concise, functional-style code. A lambda expression is an anonymous function that can be used to implement a method defined by a functional interface. Functional interfaces are interfaces that define only a single abstract method. Lambda expressions can be used to implement this abstract method, without having to define an entire new class that implements the interface. One question that arises when working with lambda expressions is how to return a value from the expression. In this article, we will explore the ways in which we can return values from a lambda expression in Java. Returning a Value from a Lambda Expression:In Java, we can return a value from a lambda expression using the arrow (->) operator. The arrow operator separates the parameters from the body of the lambda expression. The return type of the lambda expression is inferred by the compiler, based on the context in which it is used. Here are two examples that demonstrate how to return a value from a lambda expression in Java: The arrow operator (->) in a lambda expression can be used to return a value from a single expression, or from a block of statements enclosed in curly braces ({ }). If the body of the lambda expression consists of a single statement, the return keyword is not required. For example: Syntax 1: Syntax 2: Example 1: Returning an integer value from a lambda expression LambaExample.java Output: The square of 5 is 25 In this example, we define a functional interface MyInterface with a method myMethod that takes an integer parameter and returns an integer. We then create a lambda expression that implements this method by calculating the square of the input parameter x and returning the result. Finally, we assign the lambda expression to an instance of MyInterface and call the myMethod method, passing in an argument of 5. The result is then printed to the console. Example 2: Returning a string value from a lambda expression LambdaExample.java Output: Hello World! In this example, we define a functional interface MyInterface with a method myMethod that takes two string parameters and returns a string. We then create a lambda expression that implements this method by concatenating the two input strings and returning the result. Finally, we assign the lambda expression to an instance of MyInterface and call the myMethod method, passing in the arguments "Hello" and "World!". The result is then printed to the console. When using lambda expressions with functional interfaces that have a void return type, the lambda expression should not return a value. Instead, it should perform the required operation or side effect. For example: LambdaExample.java Output: The value of x is: 5 It is important to remember that lambda expressions are not objects, but rather a way of defining behavior. Therefore, they cannot be instantiated or stored in variables directly. Instead, they can only be assigned to functional interface types. This means that the interface type determines the signature of the lambda expression, and therefore the number and types of its parameters and return value. Finally, lambda expressions are a key feature of the functional programming paradigm in Java. They enable the use of higher-order functions, which are functions that take other functions as arguments or return functions as results. This can lead to more concise and expressive code, as well as improved performance through parallelization and lazy evaluation. Conclusion:Lambda expressions in Java provide a concise and powerful way of writing functional-style code. In this article, we have seen how to return values from a lambda expression using the arrow operator. By using lambda expressions, we can write code that is more readable and maintainable than traditional imperative-style code. Next Topicif Condition in Lambda Expression Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India