Currently, I'm facing with below dataset. My aim is to get the latest sum of Column4 group by the first two-column.
// Column5 = version
new Foo(1, "bbb", "cccc", 111, 0)
new Foo(1, "bbb", "cccc", 234, 1) // latest
new Foo(1, "bbb", "dddd", 111, 0)
new Foo(1, "bbb", "dddd", 112, 1)
new Foo(1, "bbb", "dddd", 113, 2)
new Foo(1, "bbb", "dddd", 114, 3) // latest
new Foo(1, "xxx", "cccc", 111, 0) // latest
new Foo(2, "xxx", "yyyy", 0, 0)
new Foo(2, "xxx", "yyyy", 1, 1) // latest
...
What I have tried is
// key: Column1, key: Column2, value: latest sum of Column4
Map<Long, Map<String, Integer>> fooMap = fooList.stream().collect(
Collectors.groupingBy(Foo::getColumn1, Collectors.groupingBy(Foo::getColumn2,
Collectors.collectingAndThen(????))));
whether the ????
part I've tried Collectors.groupingBy
, Collectors.maxBy
, Collectors.summingInt
but it is always wrong.
My ideal Map should be something like below:
1->bbb->348
,1->xxx->111
, 2->xxx->1
.
Please help Let me know if any supplement wants to have. Thanks.