title: Java 8 stream groupingBy. How to set field if only one object of grouping value?
body: <p>I have Class </p>
  
  <pre><code>public class SomeClass {
  
      private String date;
      private int players;
      private int currency;
  
  // getters setters omitted for brevity
  }
  </code></pre>
  
  <p>I have collection of this objects. Now I want to group by date this objects and sum players count and set currencyId to 0. I do it by this way:</p>
  
  <pre><code>list = list.stream()
             .collect(
                      groupingBy(SomeClass::getDate,
                                collectingAndThen(reducing((a, b) -&gt; {
                                    a.setDate(a.getDate());
                                    a.setPlayers(a.getPlayers() + b.getPlayers());
                                    a.setCurrency(0);
                                    return a;
                                }), Optional::get)))
             .values();
  </code></pre>
  
  <p>Every thing is fine except when I have only one object of certain date. That object doesnt set currency to 0 (because reducing don`t work if object is only one.) So this is my problem.</p>
  
  <p>If I have: </p>
  
  <pre><code>   Object1 ("11.09", 12, 12)
     Object2 ("11.09", 8, 13)
     Object3 ("12.09", 1, 2)
     Object4 ("12.09", 0, 1)
     Object5 ("13.09", 12, 12)
  </code></pre>
  
  <p>The output must be: </p>
  
  <pre><code>   Object6 ("11.09", 20, 0)
     Object7 ("12.09", 1, 0)
     Object8 ("13.09", 12, **0**)
  </code></pre>
  
  <p>But instead:</p>
  
  <pre><code>   Object6 ("11.09", 20, 0)
     Object7 ("12.09", 1, 0)
     Object8 ("13.09", 12, **12**)
  </code></pre>
  
  <p>Help pls</p>