title: Convert forEach in Java to map collect
body: <p>I have nested map called aMap for which key is a string and value is another map (called y). This y contains strings as key and value. I want to process the values in the nested map and aggregate some of the keys inside y according to some logic (basically I am reducing the number of keys in y). I am able to run my logic using forEach but I need to convert the following using map and collect in Java 8. The problem is I am not sure what to write inside the collect function. </p>
  
  <p>The for each code:</p>
  
  <pre><code>aMap.forEach(
        (x, y) -&gt; {
          Set&lt;String&gt; remove = new HashSet&lt;&gt;();
          Map&lt;String, Double&gt; add = new HashMap&lt;&gt;();
  
          y.forEach(
              (key, value) -&gt; {
                String z = key;
                long b = 0;
                try {
                  b = some_conversion_on_z()
                } catch (ParseException e) {
                  logger.error("Failed to convert", e);
                }
                if (some_condition_on_b) {
                  String d = some_conversion_on_z()
                  Double currCount = value;
                  Double prevCount = y.get(d);
                  if (prevCount == null) {
                    prevCount = 0.0;
                    add.put(d, prevCount + currCount);
                  } else {
                    y.put(d, prevCount + currCount);
                    remove.add(z);
                  }
                }
              });
          y.keySet().removeAll(remove);
          y.putAll(add);
        });
  </code></pre>
  
  <p>The map-collect that I am trying:</p>
  
  <pre><code>aMap.entrySet()
  .map(x -&gt; x.getValue())
  .map(y -&gt; y.getKey())
  .filter(some_condition_on_y)
  .collect(?)
  </code></pre>