Hi I'm getting the error while executing my code:
java.lang.IllegalStateException: Duplicate key ad.ecs.core.scriptengine.SiteIDAndNPI@3a75ce6f
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1253)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at ad.ecs.core.scriptengine.Utils.getInterfaceIDsWithSiteIDAndNPI(Utils.java:689)
I'm trying to group up my list into the map:
public static Map<Integer, SiteIDAndNPI> getInterfaceIDsWithSiteIDAndNPI(Session session) {
@SuppressWarnings("unchecked")
List<Srvsetup> srvsetups = session.createCriteria(Srvsetup.class)
.add(Restrictions.gt("scriptinterfaceid", 0))
.list();
Map<Integer, SiteIDAndNPI> interfaceIDs = srvsetups.stream()
.collect(Collectors.toMap(srv -> srv.getScriptinterfaceid(),
srv -> getSiteIDAndNPI(srv.getId().getSiteid(), session)));
@SuppressWarnings("unchecked")
List<Facsetup> facsetups = session.createCriteria(Facsetup.class)
.add(Restrictions.gt("scriptinterfaceid", 0))
.list();
interfaceIDs.putAll(facsetups.stream()
.collect(Collectors.toMap(fac -> fac.getScriptinterfaceid(),
fac -> getSiteIDAndNPI(fac.getPlid(), session))));
return interfaceIDs;
}
public static SiteIDAndNPI getSiteIDAndNPI(int siteID, Session session) {
SiteIDAndNPI siteIDAndNPI = new SiteIDAndNPI();
siteIDAndNPI.setSiteID(siteID);
Mdsetup1 mdsetup1 = (Mdsetup1) session.get(Mdsetup1.class, siteID);
if (mdsetup1 != null)
siteIDAndNPI.setNPI(mdsetup1.getProviderId());
return siteIDAndNPI;
}
I looked at this link: Java 8 List<V> into Map<K, V>
There is a suggestion there to use group by. But I do not want to get map like this:
Map<Integer, List<SiteIDAndNPI>>
Instead I would like to override duplicate keys. Is it achievable without doing some ugly code?