 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
How to 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
 