Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java 8: 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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Thanks!

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading