title: Java 8 filter List by highest values of each id
body: <p>Is there a way to do the following with java 8 features and lambdas?</p>
  
  <p>I have this class</p>
  
  <pre><code>public class A{
      private String id;
      private int version;
      // getters...
  }
  </code></pre>
  
  <p>Input (the list is ordered by id and versions ascending):</p>
  
  <pre><code>[{"1",1},{"1",2},{"2",1},{"2",2}]
  </code></pre>
  
  <p>I want to have a List with the highest versions of each id, so the result should look like:</p>
  
  <pre><code>[{"1",2},{"2",2}]
  </code></pre>
  
  <p>I already have a solution but I dont like it that much. I figured maybe there is a better way with java 8.</p>
  
  <pre><code>static List&lt;A&gt; removeOldVersions(List&lt;A&gt; aList) {
      Map&lt;String, A&gt; map = new HashMap&lt;&gt;();
      aList.forEach(a -&gt; map.put(a.getId(), a));
      return (List&lt;A&gt;) map.values();
  }
  </code></pre>