title: Java 8 stream - merge collections of objects sharing the same Id
body: <p>I have a collection of invoices :</p>
  
  <pre><code>class Invoice {
    int month;
    BigDecimal amount
  }
  </code></pre>
  
  <p>I'd like to merge these invoices, so I get one invoice per month, and the amount is the sum of the invoices amount for this month.</p>
  
  <p>For example:</p>
  
  <pre><code>invoice 1 : {month:1,amount:1000}
  invoice 2 : {month:1,amount:300}
  invoice 3 : {month:2,amount:2000}
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>invoice 1 : {month:1,amount:1300}
  invoice 2 : {month:2,amount:2000}
  </code></pre>
  
  <p>How can I do this with java 8 streams?</p>
  
  <p>EDIT : as my Invoice class was mutable and it was not a problem to modify them, I choosed Eugene's solution</p>
  
  <pre><code>Collection&lt;Invoice&gt;  invoices = list.collect(Collectors.toMap(Invoice::getMonth, Function.identity(), (left, right) -&gt; {
                  left.setAmount(left.getAmount().add(right.getAmount()));
                  return left;
              })).values();
  </code></pre>