- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 can we pass lambda expression in a method in Java?
A lambda expression passed in a method that has an argument of type of functional interface. If we need to pass a lambda expression as an argument, the type of parameter receiving the lambda expression argument must be of a functional interface type.
In the below example, the lambda expression can be passed in a method which argument's type is "TestInterface".
Example
interface TestInterface { boolean test(int a); } class Test { // lambda expression can be passed as first argument in the check() method static boolean check(TestInterface ti, int b) { return ti.test(b); } } public class LambdaExpressionPassMethodTest { public static void main(String arg[]) { // lambda expression boolean result = Test.check((x) -> (x%2) == 0, 10); System.out.println("The result is: "+ result); } }
Output
The result is: true
Advertisements