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.
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.
The only way you can do this if you have fixedValue as a class level variable and you dont need to supply it in the function. In other words
public class Foo {
private Integer fixedValue;
public Integer binaryFunction(Integer a) {
//use fixedValue (as it is class variable)
//some stuff
}
public void doSomeThing() {
this.fixedValue = ...//setting fixed value here
List<Integer> otherValues = ...
otherValues.map(this::binaryFunction)...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With