I have question about passing method reference as argument in java (util) functions.
I have two functions
Function<Value, Output> f1 = (val) -> {
Output o = new Output();
o.setAAA(val);
return o;
};
Function<Value, Output> f2 = (val) -> {
Output o = new Output();
o.setBBB(val);
return o;
};
I want to merge them into one function which should looked like
BiFunction<MethodRefrence, Value, Output> f3 = (ref, val) -> {
Output o = new Output();
Output."use method based on method reference"(val);
return o;
};
I want to use this function like
f3.apply(Output::AAA, number);
Is it possible ? I can't figure out correct syntax, how to make such a function.
It looks like you want a function like
BiFunction<BiConsumer<Output,Value>, Value, Output> f = (func, val) -> {
Output o = new Output();
func.accept(o, val);
return o;
};
which you can invoke like
f.apply(Output::setAAA, val);
f.apply(Output::setBBB, val);
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