title: Java 8 List to Map with stream (avoid duplicates)
body: <p>I have two classes:</p>
  
  <pre><code>public class Adegae implements java.io.Serializable {
      private AdegaeId id;
      ...
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class AdegaeId implements java.io.Serializable {
  
      private String gpi;
      private String mccode;
      private String aecode;
      ...
  }
  </code></pre>
  
  <p>I also have a list that have elements with duplaicates of <code>aecode</code>:</p>
  
  <pre><code>List&lt;Adegae&gt; adegaes = ...
  </code></pre>
  
  <p>I'm building my map like so:</p>
  
  <pre><code>Map&lt;String, Adegae&gt; adegaeMap = adegaes.stream()
              .collect(Collectors.toMap(adegae -&gt; adegae.getId().getAecode().trim(), 
                      Function.identity()));
  </code></pre>
  
  <p>When I run my code I get this exception:</p>
  
  <pre><code>java.lang.IllegalStateException: Duplicate key my.package.Adegae@678e3d87
  at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
  at java.util.HashMap.merge(HashMap.java:1245)
  at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
  at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
  </code></pre>
  
  <p>I wonder if there is a way to collect to a Map by dropping the duplicates at the same time.</p>