Search code examples
javalambdajava-stream

Two objects supplier


I have a binary function:

public Integer binaryFunction(Integer a, Integer b){
    //some stuff
}

And I have this stream:

Integer fixedValue = ...
List<Integer> otherValues = ...
otherValues.map(value -> binaryFunction(value, fixedValue)).collect(...);

I would like to know how can I use map(this::binaryFunction) in my stream. I tried something like this but it won't work:

values.map(value -> (value, fixedValue)).map(this::binaryFunction).collect(...);

But it does not work. I'm looking for a supplier that can take two values.

Note: I don't want to change binaryFunction declaration.


Solution

  • You can't do this. You must use the lambda you have shown.

    You could write a function that modifies binary functions to fix their first argument, but its body is just going to be the same lambda.