- 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 to pass a lambda expression as a method parameter in Java?
A lambda expression is an anonymous or unnamed method in Java. It doesn't execute on its own and used to implement methods that are declared in a functional interface. If we want to pass a lambda expression as a method parameter in java, the type of method parameter that receives must be of functional interface type.
Example
interface Algebra { int operate(int a, int b); } enum Operation { ADD, SUB, MUL, DIV } public class LambdaMethodArgTest { public static void main(String[] args) { print((a, b) -> a + b, Operation.ADD); print((a, b) -> a - b, Operation.SUB); print((a, b) -> a * b, Operation.MUL); print((a, b) -> a / b, Operation.DIV); } static void print(Algebra alg, Operation op) { switch (op) { case ADD: System.out.println("The addition of a and b is: " + alg.operate(40, 20)); break; case SUB: System.out.println("The subtraction of a and b is: " + alg.operate(40, 20)); break; case MUL: System.out.println("The multiplication of a and b is: " + alg.operate(40, 20)); break; case DIV: System.out.println("The division of a and b is: " + alg.operate(40, 20)); break; default: throw new AssertionError(); } } }
Output
The addition of a and b is: 60 The subtraction of a and b is: 20 The multiplication of a and b is: 800 The division of a and b is: 2
- Related Articles
- Java Program to pass lambda expression as a method argument
- How can we pass lambda expression in a method in Java?
- How to pass a function as a parameter in Java
- How to pass a jQuery event as a parameter in a method?
- How to write the comparator as a lambda expression in Java?
- How to pass a 2D array as a parameter in C?
- Pass long parameter to an overloaded method in Java
- How can we write Callable as a lambda expression in Java?
- How to pass an object as a parameter in JavaScript function?
- How to pass an array as a URL parameter in Laravel?
- How to write a conditional expression in lambda expression in Java?
- How to pass a json object as a parameter to a python function?
- How to declare a variable within lambda expression in Java?
- How to reverse a string using lambda expression in Java?
- How to use a return statement in lambda expression in Java?

Advertisements