Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 605
Victoria Antolucci Avatar asked Dec 11 '25 16:12

Victoria Antolucci


2 Answers

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.

like image 156
Louis Wasserman Avatar answered Dec 14 '25 06:12

Louis Wasserman


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)...
    }
}
like image 37
StackFlowed Avatar answered Dec 14 '25 04:12

StackFlowed