How do I pass a lambda expression with arguments as parameters in Java 8?

Here's what I've tried. and it doesn't even compile.

public class LambdaExample {

    public static Integer handleOperation(Integer x, Integer y,  Function converter){
                return converter.apply(x,y);
    }

    public static void main(String[] args){
         handleOperation(10,10, Operation::add);
    }

}

class Operation {

    public int add(Integer x, Integer y){
        return x+y;
    }

}

      

Several things I am trying to get / find out here:

1) how to pass lambda expression

as a method parameter (in the method main

above)

2) how to pass parameters to a function (in a method handleOpertion

, there is an error compilation

applying only one parameter)

+3


source to share


3 answers


Your method handleOperation

accepts an object that implements Function

, Operation::add

(a reference to the method) is not suitable. Also, for two arguments, you will need to use BiFunction

.

Here's an example that should work:

public class LambdaExample {

    public static Integer handleOperation(Integer x, Integer y,  BiFunction<Integer, Integer, Integer> converter){
                return converter.apply(x,y);
    }

    public static void main(String[] args){
         handleOperation( 10,10, new Operation() ); // should return 20
    }

}

class Operation implements BiFunction<Integer, Integer, Integer> {

    public Integer apply(Integer x, Integer y){
        return x+y;
    }

}

      



Updated:

public class LambdaExample {

    public static Integer handleOperation(Integer x, Integer y,  BiFunction<Integer, Integer, Integer> converter){
                return converter.apply(x,y);
    }

    public static void main(String[] args){
         handleOperation( 10,10, Operation::add ); // should return 20
    }

}

class Operation {

    public static int add(Integer x, Integer y){
        return x+y;
    }

}

      

+4


source


A Function

takes input x and gives the result y. So you are not looking for Function

(let alone that you were using the raw type) when doing return converter.apply(x,y);

, but for BiFunction<Integer, Integer, Integer>

or simpler, a BinaryOperator<Integer>

, since each type parameter is identical.

1) how to pass lambda expression as method parameter (in main method above)

By providing a lambda expression that conforms to the interface contract BinaryOperator<Integer>

, i.e. a method that takes two parameters Integer

and returns Integer

.

handleOperation(10,10, (a, b) -> a + b)

      

2) how to pass parameters to the function (in the handleOpertion method, there is a compilation error that applies, takes only one parameter)



Since the function is of the form f => u

, so the apply method takes one argument and gives one result, like a mathematical function such as f(x) = 2 * x

(see first part of the answer).

Here's what I've tried. and it doesn't even compile.

To compile your code, you can make the method static, or create a new instance before using the method reference. It will then refer to the add

new instance's handleOperation

method when it calls the function's apply method.

handleOperation(10,10, new Operation()::add);

      

Note that this method already exists in the JDK, it is Integer::sum

. It accepts two primitive int values ​​instead of references Integer

, but it's close enough that the auto-boxing mechanism will make this method valid to look like BinaryOperator<Integer>

in the context of the method.

+5


source


The Function parameter is raw (untyped) and must be BiFunction.

Try the following:

public static Integer handleOperation(Integer x, Integer y,  BiFunction<Integer, Integer, Integer> converter){
    return converter.apply(x,y);
}

      

BiFunction with all three types can replace one (typical) BinaryOperator:

public static Integer handleOperation(Integer x, Integer y,  BinaryOperator<Integer> converter){
    return converter.apply(x,y);
}

      

To call it, you can do this:

int sum = handleOperation(1, 2, (x, y) -> x + y); // 3

      

In fact, you have implemented a reduction. This call could also be written as:

int sum = Stream.of(1, 2).reduce((x, y) -> x + y);

      

+3


source







All Articles