I have two classes:
public class Adegae implements java.io.Serializable {
private AdegaeId id;
...
}
and
public class AdegaeId implements java.io.Serializable {
private String gpi;
private String mccode;
private String aecode;
...
}
I also have a list that have elements with duplaicates of aecode
:
List<Adegae> adegaes = ...
I'm building my map like so:
Map<String, Adegae> adegaeMap = adegaes.stream()
.collect(Collectors.toMap(adegae -> adegae.getId().getAecode().trim(),
Function.identity()));
When I run my code I get this exception:
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)
I wonder if there is a way to collect to a Map by dropping the duplicates at the same time.