title: Converting a map into another map using the java 8 stream API
body: <p>Say I have the following map:</p>
  
  <pre><code>Map&lt;Member, List&lt;Message&gt;&gt; messages = ... //constructed somehow
  </code></pre>
  
  <p>I would like to use the java 8 stream api in order to obtain a:</p>
  
  <pre><code>SortedMap&lt;Message, Member&gt; latestMessages = ...
  </code></pre>
  
  <p>Where the comparator passed into the SortedMap/TreeMap would be based on the message sendDate field.</p>
  
  <p>Furthermore, of the list of sent messages, I would select the latest message which would become the key to the sorted map.</p>
  
  <p>How can I achieve that?</p>
  
  <p><strong>edit 1</strong>:</p>
  
  <pre><code>Comparator&lt;Message&gt; bySendDate = Comparator.comparing(Message::getSendDate);
  SortedMap&lt;Message, Member&gt; latestMessages = third.entrySet().stream()
          .collect(Collectors.toMap(e -&gt; e.getValue().stream().max(bySendDate).get(), Map.Entry::getKey, (x, y) -&gt; {
              throw new AssertionError();
          }, () -&gt; new TreeMap(bySendDate.thenComparing(Comparator.comparing(Message::getId)))));
  </code></pre>
  
  <p>I get the following compilation error:</p>
  
  <pre><code>The method collect(Collector&lt;? super T,A,R&gt;) in the type Stream&lt;T&gt; is not applicable for the arguments (Collector&lt;Map.Entry&lt;Member,List&lt;Message&gt;&gt;,?,TreeMap&gt;)
  </code></pre>