• java.lang.IllegalStateException: Duplicate key 20


    这个我在公司遇到的一个问题。原因:
    使用Map<String, String> RelationMap = relation.stream().collect(Collectors.toMap(s -> s[2], s -> s[1], (oldValue, newValue) -> newValue)))
    转换过程中出现重复的Key。导致有多个value程序不知道应该取哪个的问题。

    正常案例

    老师跟班级之间的关系,每一个老师都负责一个班级。

     1 @Data
     2 public class TeacherClass {
     3 
     4     /**主键ID*/
     5     private int id;
     6 
     7     /**教师ID*/
     8     private int teachId;
     9 
    10     /**班级ID*/
    11     private int classId;
    12 
    13     public TeacherClass(int id, int teachId, int classId) {
    14         this.id = id;
    15         this.teachId = teachId;
    16         this.classId = classId;
    17     }
    18 }


    老师与班级的关系

    实际应用代码

     1 //查询所有老师负责的班级(一个老师对应一个班级)
     2         List<TeacherClass> list = new ArrayList<>(20);
     3         int count = 10;
     4         for (int i = 0; i < count; i++) {
     5             list.add(new TeacherClass(i, 100 + i, 200 + i));
     6         }
     7 
     8         //结果返回每个老师对应的班级ID
     9         Map<Integer, Integer> collect = list.stream().collect(Collectors.toMap(TeacherClass::getTeachId, TeacherClass::getClassId));
    10         System.out.println("老师负责的班级ID:" + collect);



    执行结果如下

    显然是OK的

    错误案例

    假设这个时候程序出了一个bug, 导致有一个老师负责两个班级。那么再执行之前的代码会有什么问题呢?

     1         //查询所有老师负责的班级(一个老师对应一个班级)
     2         List<TeacherClass> list = new ArrayList<>(20);
     3         int count = 10;
     4         for (int i = 0; i < count; i++) {
     5             list.add(new TeacherClass(i, 100 + i, 200 + i));
     6         }
     7 
     8         //程序出了一个bug,导致有一个老师负责两个班级
     9         list.add(new TeacherClass(50, 100, 300));
    10 
    11         //结果返回每个老师对应的班级ID
    12         Map<Integer, Integer> collect = list.stream().collect(Collectors.toMap(TeacherClass::getTeachId, TeacherClass::getClassId));
    13         System.out.println("老师负责的班级ID:" + collect);

    执行结果如下

    显然是有问题的。原因就是出现了重复的Key, 那么怎么解决呢?
    很简单,将Collectors.toMap(TeacherClass::getTeachId, TeacherClass::getClassId) 替换为Collectors.toMap(TeacherClass::getTeachId, TeacherClass::getClassId, (aLong, aLong2) -> aLong2 >= aLong ? aLong2 : aLong);

    (aLong, aLong2) -> aLong2 >= aLong ? aLong2 : aLong表示的是出现重复Key的执行逻辑,如果出现重复Key取最大的value。具体逻辑可以自己定义

     

    实际应用代码

     1 //查询所有老师负责的班级(一个老师对应一个班级)
     2         List<TeacherClass> list = new ArrayList<>(20);
     3         int count = 10;
     4         for (int i = 0; i < count; i++) {
     5             list.add(new TeacherClass(i, 100 + i, 200 + i));
     6         }
     7 
     8         //程序出了一个bug,导致有一个老师负责两个班级
     9         list.add(new TeacherClass(50, 100, 300));
    10 
    11         //结果返回每个老师对应的班级ID
    12         Map<Integer, Integer> collect = list.stream().collect(Collectors.toMap(TeacherClass::getTeachId, TeacherClass::getClassId
    13                 , (aLong, aLong2) -> aLong2 >= aLong ? aLong2 : aLong));
    14         System.out.println("老师负责的班级ID:" + collect);

    至此问题就解决了

  • 相关阅读:
    centos7之防止root密码被破解
    近期codeforces做题的总结?(不定期更新)
    小程序分享微信好友
    小程序自定义头部导航栏滑动颜色渐变
    小白快速上手的react.js教程
    架构型设计模式-同步模式
    仿vue-cli写一个简易的脚手架
    VUE基础知识篇-vue从零开始学VUE
    彻底理解Vue组件间7种方式通信
    设计模式--观察者模式
  • 原文地址:https://www.cnblogs.com/IT-study/p/11314619.html
Copyright © 2020-2023  润新知