IllegalStateException: Duplicate key

通过Java8的语法可将实体list转成 指定key的map,当key出现重复值的时候会报“IllegalStateException: Duplicate key” 的异常

解决方法:

//b覆盖
allEmployees.stream()
            .collect(Collectors.toMap(CompanyEntity::getEmployeeId, 
                 Function.identity(), (a,b) -> b))
//保存a,不覆盖
return this.fsRepo.getDosIdsWithDcs(dosIds).
        stream().collect
                (Collectors.toMap(YourModelDosClass::getDosId, v->v,
                        (v1,v2) -> v1));

猜你喜欢

转载自blog.csdn.net/SHUKAI618/article/details/123910658
今日推荐