
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use a Return Statement in Lambda Expression in Java
A return statement is not an expression in a lambda expression. We must enclose statements in braces ({}). However, we do not have to enclose a void method invocation in braces. The return type of a method in which lambda expression used in a return statement must be a functional interface.
Example 1
public class LambdaReturnTest1 { interface Addition { int add(int a, int b); } public static Addition getAddition() { return (a, b) -> a + b; // lambda expression return statement } public static void main(String args[]) { System.out.println("The addition of a and b is: " + getAddition().add(20, 50)); } }
Output
The addition of a and b is: 70
Example 2
public class LambdaReturnTest2 { public static void main(String args[]) { Thread th = new Thread(getRunnable()); th.run(); } public static Runnable getRunnable() { return() -> { // lambda expression return statement System.out.println("Lambda Expression Return Statement"); }; } }
Output
Lambda Expression Return Statement
Advertisements