- Trending Categories
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
- Related Articles
- How to pass a lambda expression as a method parameter in Java?
- How can we write a multiline lambda expression in Java?
- Java Program to pass lambda expression as a method argument
- How can we write Callable as a lambda expression in Java?
- What kind of variables can we access in a lambda expression in Java?
- How many parameters can a lambda expression have in Java?
- How can we iterate the elements of List and Map using lambda expression in Java?
- How to write a conditional expression in lambda expression in Java?
- Differences between Lambda Expression and Method Reference in Java?
- How do we pass parameters by value in a Java method?
- Can we sort a list with Lambda in Java?
- Lambda expression in Java 8
- How to declare a variable within lambda expression in Java?
- How to reverse a string using lambda expression in Java?
- How to use BooleanSupplier in lambda expression in Java?

Advertisements