项目重启后突然报这个异常
看日志应该是在初始化字典,源代码
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://www.toymoban.com/news/detail-605798.html
参考:https://blog.csdn.net/weixin_40873693/article/details/124659750文章来源地址https://www.toymoban.com/news/detail-605798.html
到了这里,关于解决java.lang.IllegalStateException: Duplicate key异常的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!