How to sort Map<YearMonth, List<LocalDate>> with java 8 lambda and streams
<p>I have a list of sorted dates like this:</p>

<pre><code>2016-07-07
2016-07-08
2016-07-09
2016-07-10
2016-07-11
2016-07-12
2016-07-13
...
2016-07-31
2016-08-01
2016-08-02
2016-08-03
...
2017-01-01
2017-01-02
2017-01-03
...
</code></pre>

<p>From this list I generate a <code>Map&lt;YearMonth, List&lt;LocalDate&gt;&gt;</code> with stream:</p>

<pre><code>Map&lt;YearMonth, List&lt;LocalDate&gt;&gt; d = dates.stream().collect(Collectors.toList())
      .stream().collect(Collectors.groupingBy(date -&gt; YearMonth.from(date)));
</code></pre>

<p>The output of that map looks like this:</p>

<pre><code>{2016-12=[2016-12-01, 2016-12-02,...2016-12-31], 2016-11=[2016-11-01, 2016-11-02,...]}
</code></pre>

<p><strong>But my needed output should look be like this:</strong></p>

<ul>
<li>Key of map sorted by date ascending: <code>{2016-07=[...], 2016-08=[...]}</code></li>
<li>Value of map (List) sorted by date ascending: <code>{2016-07=[2016-07-01, 2016-07-02, ...], 2016-08=[2016-08-01, 2016-08-02, ...]}</code></li>
</ul>

<p>I tried many options to get my expected result but I just get the correct sorting for <strong>the key or the value and not for both</strong>:</p>

<pre><code>Map&lt;YearMonth, List&lt;LocalDate&gt;&gt; m = stream().collect(Collectors.toList())
      .stream().sorted((e1,e2) -&gt; e2.compareTo(e1))
      .collect(Collectors.groupingBy(date -&gt; YearMonth.from(date)));
</code></pre>

<p>Result: </p>

<pre><code>{2016-07=[2016-07-31, 2016-07-30, ...], 2016-08=[2016-08-31, 2016-08-30, ...]}
</code></pre>

<p><strong>How can I sort both by key and value?</strong></p>