java pass lambda as parameter
Lambda is a powerful thing introduced in Java 8 and since it’s launch it has becoming more and more common in use by the Java developers. Major reason behind switching to Java 8 is to get into the Lambda world. In this post, you will learn How to Pass a Lambda as Parameter to another method?
Pass Lambda as Parameter in Java...!!! Share on X
What is Lambda Expression?
A lambda expression is an anonymous method which takes in parameters and returns a value. As it’s just an expression, It doesn’t execute by itself and therefore it’s basically used to implement methods of a Functional Interface.
Few Examples:
1 | i -> i + 10; |
1 | (i, j) -> i + j; |
1 | (i, j) -> {System.out.println(i-j);} |
How to Pass Lambda expression as Method Parameter?
In order to pass Lambda expression as a Method Parameter, the type of method parameter that receives must be of type of the Functional Interface.
Example: Arithmetic Operations using Lambda Expression
This is the easiest example on how to pass a Lambda as a parameter to another method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /** * Pass Lambda as Method Parameter * @author Deepak Verma * */ interface Arithmetic { int operation(int a, int b); } public class Pass_Lambda_As_Method_Parameter { public static void main(String[] args) { int a = 60; int b = 24; Arithmetic doAdd = (i,j) -> i + j; //Lambda Expression for Addition Arithmetic doSub = (i,j) -> i - j; //Lambda Expression for Subtraction Arithmetic doMul = (i,j) -> i * j; //Lambda Expression for Multiplication Arithmetic doDiv = (i,j) -> i / j; //Lambda Expression for Division //Pass Lambda Expression to another Method printAdd(doAdd,a,b); printAdd(doSub,a,b); printAdd(doMul,a,b); printAdd(doDiv,a,b); } public static void printAdd(Arithmetic x, int n1, int n2) { System.out.println("The Addition of "+n1+" and "+n2+": " + x.operation(n1, n2)); } public static void printSubtract(Arithmetic x, int n1, int n2) { System.out.println("The Subtraction of "+n1+" and "+n2+": " + x.operation(n1, n2)); } public static void printMultiply(Arithmetic x, int n1, int n2) { System.out.println("The Multiplication of "+n1+" and "+n2+": " + x.operation(n1, n2)); } public static void printDivide(Arithmetic x, int n1, int n2) { System.out.println("The Division of "+n1+" and "+n2+": " + x.operation(n1, n2)); } } |
Let’s understand the code:
1. Here we have a functional interface (Arithmetic) which has a method (Operation) that takes two integers and return an integer.
2. Now, let’s create Lambda Expression which takes two integers and return an integer after (adding / Subtracting / Multiplying / Dividing) those two integers.
1 | Arithmetic doAdd = (i,j) -> i + j; |
3. Finally, Create a static method (Print) that takes Lambda expression as an argument along with two integer values and Implement the method (Operation) of the Interface (Arithmetic) using that Lambda Expression.
1 2 3 | public static void printAdd(Arithmetic x, int n1, int n2) { System.out.println("The Addition of "+n1+" and "+n2+": " + x.operation(n1, n2)); } |
4. Then, just print the values using Sysout operation.
Output
1 2 3 4 | The Addition of 60 and 24: 84 The Addition of 60 and 24: 36 The Addition of 60 and 24: 1440 The Addition of 60 and 24: 2 |
java pass lambda as parameter. java pass lambda as parameter java pass lambda as parameter java pass lambda as parameter java pass lambda as parameter java pass lambda as parameter java pass lambda as parameter pass lambda as parameter java pass lambda as parameter java how to pass lambda as a parameter in java
I hope you understand now, how to pass a Lambda expression as a parameter to another method.
This was the simplest example to make you clear enough on the working of the Lambdas in Java.
java pass lambda as parameter
Pass Lambda as Parameter in Java...!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Supplier Interface in Java 8 with examples
- Consumer Interface in Java 8 with examples
- Predicate Interface in Java 8 with examples
- BiConsumer Interface in Java 8 with examples
- Function Interface in Java 8 with examples
- BiFunction Interface in Java 8 with examples
- DoubleConsumer Interface in Java 8 with examples
- BooleanSupplier Interface in Java 8 with examples
- DoubleFunction Interface in Java 8 with examples
- DoublePredicate Interface in Java 8 with examples
- BiPredicate Interface in Java 8 with examples
- DoubleToIntFunction Interface in Java 8 with examples
- IntToDoubleFunction Interface in Java 8 with examples
- DoubleSupplier Interface in Java 8 with examples
- LongToIntFunction Interface in Java 8 with examples
- DoubleToLongFunction Interface in Java 8 with examples
- IntToLongFunction Interface in Java 8 with examples
- LongToDoubleFunction Interface in Java 8 with examples
- IntConsumer Interface in Java 8 with examples
Other Useful References: