解决java.lang.IllegalStateException: Duplicate key异常
·
项目重启后突然报这个异常
看日志应该是在初始化字典,源代码
private Map<String, String> dictMap;
@PostConstruct
publicvoid init() {
List<SysDictData> eventType = DictUtils.getDictCache("xxx");
dictMap = eventType.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
}
这里的操作是把词典list转换成map,然后key冲突。
但我比对了一下数据,没有找到重复的dictValue,报这个错有点莫名其妙。
最后的解决办法参考了其他网友,得以顺利解决,最后上修改后的代码
private Map<String, String> dictMap;
@PostConstruct
public void init() {
if (dictMap == null || dictMap.isEmpty()) {
List<SysDictData> eventType = DictUtils.getDictCache("xxx");
dictMap = eventType.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel, (entity1, entity2) -> entity1));
}
}
Collectors.toMap
增加了第三个参数(entity1, entity2) -> entity1)
,这个参数的意思是如果entity1、entity2的key值相同,选择entity1作为那个key所对应的value值。
参考:https://blog.csdn.net/weixin_40873693/article/details/124659750
更多推荐
相关推荐
查看更多
鸿蒙开发工具大赶集

本仓将收集和展示鸿蒙开发工具,欢迎大家踊跃投稿。通过pr附上您的工具介绍和使用指南,并加上工具对应的链接,通过的工具将会成功上架到我们社区。
OpenManus

No fortress, purely open ground. OpenManus is Coming.
G-Star公益行

G-Star 公益行 是 GitCode G-Star 计划旗下专为公益机构打造的技术赋能计划,依托 GitCode 开源平台、生态流量、云计算与 AI 支持,旨在连接开源技术与公益组织,通过技术赋能帮助公益组织实现数字化转型,以提升运营效率、优化资源配置、拓展公益影响力。
所有评论(0)