title: Java 8 stream sum entries for duplicate keys
body: <p>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)</p>
  
  <pre><code>class Thing {
      private String key;
      private Date activityDate;
      private float value;
      ...
  }
  </code></pre>
  
  <p>Then I'm grouping them like so:</p>
  
  <pre><code>Map&lt;String, List&lt;Thing&gt;&gt; thingsByKey = thingList.stream().collect(
                  Collectors.groupingBy(
                          Thing::getKey,
                          TreeMap::new,
                          Collectors.mapping(Function.identity(), toSortedList())
                  ));
  
  private static Collector&lt;Thing,?,List&lt;Thing&gt;&gt; toSortedList() {
          return Collectors.collectingAndThen(toList(),
                  l -&gt; l.stream().sorted(Comparator.comparing(Thing::getActivityDate)).collect(toList()));
      }
  </code></pre>
  
  <p>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,</p>
  
  <p>Thing1
  Date=1/1/2017
  Value=10</p>
  
  <p>Thing2
  Date=1/1/2017
  Value=20</p>
  
  <p>Turns into 30 for 1/1/2017.</p>
  
  <p>What's the best way to accomplish something like that?</p>