Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define a method which takes a lambda as a parameter in Java 8?

In Java 8, methods can be created as Lambda expressions and can be passed by reference (with a little work under the hood). There are plenty of examples online with lambdas being created and used with methods, but no examples of how to make a method taking a lambda as a parameter. What is the syntax for that?

MyClass.method((a, b) -> a+b);   class MyClass{   //How do I define this method?   static int method(Lambda l){     return l(5, 10);   } } 
like image 726
Marius Avatar asked Nov 28 '12 12:11

Marius


People also ask

How do you pass a lambda function as a parameter in Java?

Passing Lambda Expressions as ArgumentsIf you pass an integer as an argument to a function, you must have an int or Integer parameter. If you are passing an instance of a class as a parameter, you must specify the class name or the object class as a parameter to hold the object.

How do you call a lambda method?

You can invoke Lambda functions directly using the Lambda console, a function URL HTTP(S) endpoint, the Lambda API, an AWS SDK, the AWS Command Line Interface (AWS CLI), and AWS toolkits.

How do you pass a method as a parameter in Java?

Information can be passed to methods as parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

What is lambda parameter in Java?

Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.


1 Answers

Lambdas are purely a call-site construct: the recipient of the lambda does not need to know that a Lambda is involved, instead it accepts an Interface with the appropriate method.

In other words, you define or use a functional interface (i.e. an interface with a single method) that accepts and returns exactly what you want.

For this Java 8 comes with a set of commonly-used interface types in java.util.function (thanks to Maurice Naftalin for the hint about the JavaDoc).

For this specific use case there's java.util.function.IntBinaryOperator with a single int applyAsInt(int left, int right) method, so you could write your method like this:

static int method(IntBinaryOperator op){     return op.applyAsInt(5, 10); } 

But you can just as well define your own interface and use it like this:

public interface TwoArgIntOperator {     public int op(int a, int b); }  //elsewhere: static int method(TwoArgIntOperator operator) {     return operator.op(5, 10); } 

Then call the method with a lambda as parameter:

public static void main(String[] args) {     TwoArgIntOperator addTwoInts = (a, b) -> a + b;     int result = method(addTwoInts);     System.out.println("Result: " + result); } 

Using your own interface has the advantage that you can have names that more clearly indicate the intent.

like image 58
Joachim Sauer Avatar answered Sep 20 '22 23:09

Joachim Sauer