I have a class Foo
with these fields:
id:int / name;String / targetCost:BigDecimal / actualCost:BigDecimal
I get an arraylist of objects of this class. e.g.:
new Foo(1, "P1", 300, 400),
new Foo(2, "P2", 600, 400),
new Foo(3, "P3", 30, 20),
new Foo(3, "P3", 70, 20),
new Foo(1, "P1", 360, 40),
new Foo(4, "P4", 320, 200),
new Foo(4, "P4", 500, 900)
I want to transform these values by creating a sum of "targetCost" and "actualCost" and grouping the "row" e.g.
new Foo(1, "P1", 660, 440),
new Foo(2, "P2", 600, 400),
new Foo(3, "P3", 100, 40),
new Foo(4, "P4", 820, 1100)
What I have written by now:
data.stream()
.???
.collect(Collectors.groupingBy(PlannedProjectPOJO::getId));
How can I do that?
Question&Answers:
os