I use Java 8 threads to group a list of records using a specific key, and then sort the groups by date. What I would like to do in addition is to βcollapseβ any two records within the group that have the same date and summarize them. I have a class like this (trimmed for example)
class Thing { private String key; private Date activityDate; private float value; ... }
Then I group them like this:
Map<String, List<Thing>> thingsByKey = thingList.stream().collect( Collectors.groupingBy( Thing::getKey, TreeMap::new, Collectors.mapping(Function.identity(), toSortedList()) )); private static Collector<Thing,?,List<Thing>> toSortedList() { return Collectors.collectingAndThen(toList(), l -> l.stream().sorted(Comparator.comparing(Thing::getActivityDate)).collect(toList())); }
What would I like to do if any two Thing entries have the same date, sum the values ββfor them and collapse them so that
thing1 Date = 1/1/2017 Value = 10
Thing2 Date = 1/1/2017 Value = 20
Turns at 30 on 1/1/2017.
What is the best way to accomplish something like this?
source share