Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 stream sum entries for duplicate keys

I am using Java 8 streams to group a list of entries by a certain key and then sorting the groups by date. What I would like to do in addition is to "collapse" any two entries within a group that have the same date and sum them up. I have a class like this (stripped down for example purposes)

class Thing {
    private String key;
    private Date activityDate;
    private float value;
    ...
}

Then I'm grouping them like so:

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 I would like to do is, if any two Thing entries have the exact same date, sum up the values for those and collapse them down such that,

Thing1 Date=1/1/2017 Value=10

Thing2 Date=1/1/2017 Value=20

Turns into 30 for 1/1/2017.

What's the best way to accomplish something like that?

like image 277
cloudwalker Avatar asked Jun 25 '17 15:06

cloudwalker


2 Answers

I have slightly change your Thing class to use LocalData and added a very simple toString:

@Override
public String toString() {
   return " value = " + value;
}

If I understood correctly, than this is what you need:

Map<String, TreeMap<LocalDate, Thing>> result = Arrays
            .asList(new Thing("a", LocalDate.now().minusDays(1), 12f), new Thing("a", LocalDate.now(), 12f), new Thing("a", LocalDate.now(), 13f))
            .stream()
            .collect(Collectors.groupingBy(Thing::getKey,
                    Collectors.toMap(Thing::getActivityDate, Function.identity(),
                            (Thing left, Thing right) -> new Thing(left.getKey(), left.getActivityDate(), left.getValue() + right.getValue()),
                            TreeMap::new)));


 System.out.println(result); // {a={2017-06-24= value = 12.0, 2017-06-25= value = 25.0}}
like image 53
Eugene Avatar answered Oct 12 '22 10:10

Eugene


This can be accomplished using the toMap collector:

Map<Date, Thing> thingsByDate = things.stream().collect(Collectors.toMap(
    Thing::getActivityDate,
    Function.identity(),
    (thing1, thing2) -> new Thing(null, thing1.getActivityDate(), thing1.getValue()+thing2.getValue())
);

You may then do with this map as you wish.

like image 20
Joe C Avatar answered Oct 12 '22 12:10

Joe C