Suppose there is class like this:
class A {
long sent;
long received;
double val; // given as max {(double)sent/someDenominator,(double)received/someDenominator}
}
of which there are number of instance references in Map<String , A>
.
Is it possible in one go, using stream API, to return instance of class A with following properties:
What would be trivial task using standard for loop and one iteration, i don't have a clue how to achieve with stream API.
You could use reduce
:
Optional<A> a = map.values()
.stream()
.reduce((a1, a2) -> new A(a1.sent + a2.sent, a1.received + a2.received, Math.max(a1.val, a2.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