title: Java 8 List<Map<String, Object>> to List<Map<String, Object>> group by key and count by value
body: <p>I have the following list of maps</p>
  
  <pre><code>List&lt;Map&lt;String, Object&gt;&gt; listBeforeGroup = new ArrayList&lt;Map&lt;String, Object&gt;&gt;();
  
      Map&lt;String, Object&gt; m1 = new HashMap&lt;String, Object&gt;();
      m1.put("company", "LG");
      m1.put("billType", "A");
      m1.put("billPeriod", "09-2018");
  
      Map&lt;String, Object&gt; m2 = new HashMap&lt;String, Object&gt;();
      m2.put("company", "LG");
      m2.put("billType", "A");
      m2.put("billPeriod", "09-2018");
  
      Map&lt;String, Object&gt; m3 = new HashMap&lt;String, Object&gt;();
      m3.put("company", "LG");
      m3.put("billType", "A");
      m3.put("billPeriod", "09-2018");
  
      Map&lt;String, Object&gt; m4 = new HashMap&lt;String, Object&gt;();
      m4.put("company", "LG");
      m4.put("billType", "B");
      m4.put("billPeriod", "01-2019");
  
      Map&lt;String, Object&gt; m5 = new HashMap&lt;String, Object&gt;();
      m5.put("company", "LG");
      m5.put("billType", "B");
      m5.put("billPeriod", "01-2019");
  
      Map&lt;String, Object&gt; m6 = new HashMap&lt;String, Object&gt;();
      m6.put("company", "Samsung");
      m6.put("billType", "A");
      m6.put("billPeriod", "10-2018");
  
      Map&lt;String, Object&gt; m7 = new HashMap&lt;String, Object&gt;();
      m7.put("company", "Samsung");
      m7.put("billType", "A");
      m7.put("billPeriod", "10-2018");
  
      Map&lt;String, Object&gt; m8 = new HashMap&lt;String, Object&gt;();
      m8.put("company", "Samsung");
      m8.put("billType", "B");
      m8.put("billPeriod", "11-2018");
  
      listBeforeGroup.add(m1);listBeforeGroup.add(m2);
      listBeforeGroup.add(m3);listBeforeGroup.add(m4);
      listBeforeGroup.add(m5);listBeforeGroup.add(m6);
  </code></pre>
  
  <p>How do I get this output?</p>
  
  <pre><code>    //Desired Output - List&lt;Map&lt;String, Object&gt;&gt;
      //{company=LG, billType=A, billPeriod=09-2018, count=3}
      //{company=LG, billType=B, billPeriod=01-2019, count=2}
      //{company=Samsung, billType=A, billPeriod=10-2018, count=2}
      //{company=Samsung, billType=B, billPeriod=11-2018, count=1}
  </code></pre>
  
  <p>I tried this, using java 8 streams, but I can't get the desired output</p>
  
  <pre><code>List&lt;Map&lt;String, Object&gt;&gt; listAfterGroup = listBeforeGroup.stream().map(m -&gt; m.entrySet().stream().collect(Collectors.toMap(p -&gt; p.getKey(), p - &gt;  p.getValue()))).collect(Collectors.toList());
  </code></pre>
  
  <p>And tried this, btw this solution gives a map but I don't want this</p>
  
  <pre><code>Map&lt;Object, Long&gt; listAfterGroup = listBeforeGroup.stream().flatMap(m -&gt; m.entrySet().stream()).collect(Collectors.groupingBy(Map.Entry::getKey,Collectors.counting()));
  </code></pre>
  
  <p>I want to group the maps by the key "billPeriod" for example, and count items by the values, then generate a new list of maps.</p>