list添加集合被覆盖,利用map求和——代码应该怎么放(二)

上次写的代码(http://blog.csdn.net/ma15732625261/article/details/78114015)似乎还有一些问题,再一次修改代码

    public List<SingleCourseScoreShowEntity> joinToSingleCoureScore(List<StudentScoreCollectEntity> studentScoreCollectEntityList, List<StudentModel> studentModelList) {


        TreeMap<String, Integer> hashMapTotalScore = new TreeMap();//存放学生总成绩

        Map<String, StudentModel> studentModelMap = new HashMap<>();//存放学生信息

        Map<String ,String> CourScoreExitst = new HashMap<>();

        //学生实体
        for (StudentModel studentModel : studentModelList) {

            studentModelMap.put(studentModel.getId(), studentModel);
        }

        //存放 课程信息+对应的成绩 的list
        List<ScoreManagerCouseScoreModel> scoreManagerCouseScoreModelArrayList=null;

        Map<String,SingleCourseScoreShowEntity> singleCourseScoreShowEntityMap = new HashMap<>();

        //学生课程成绩实体  studentScoreCollectEntity 一个课程为一个实例集合
        for (StudentScoreCollectEntity studentScoreCollectEntityForWeb : studentScoreCollectEntityList) {

            String studentId = studentScoreCollectEntityForWeb.getStudentId();

            //承接拼写的字符串 存放学生课程的相关信息
            SingleCourseScoreShowEntity singleCourseScoreShowEntity =singleCourseScoreShowEntityMap.get(studentId);

            //返回值  singleCourseScoreShowEntity 如果不存在则新建 否则 空指针
            if (singleCourseScoreShowEntity == null ){

                singleCourseScoreShowEntity = new SingleCourseScoreShowEntity();

                if(CourScoreExitst.containsKey(studentId)==false){

                    scoreManagerCouseScoreModelArrayList =new ArrayList<>();

                }
            }

            if(scoreManagerCouseScoreModelArrayList == null){

                scoreManagerCouseScoreModelArrayList = new ArrayList<>();
            }

            //获取学生信息list 中 与当前学生课程成绩实体相关 的model
            StudentModel studentModel = studentModelMap.get(studentId);

            if (studentModel != null) {

                // 姓名
                singleCourseScoreShowEntity.setStudentName(studentModel.getName());
                //学号
                singleCourseScoreShowEntity.setStudentCode(studentModel.getStudentCode());

                //存放 课程信息+对应的成绩
                ScoreManagerCouseScoreModel scoreManagerCouseScoreModel = new ScoreManagerCouseScoreModel();

                //存放课程名称
                scoreManagerCouseScoreModel.setCourseName(studentScoreCollectEntityForWeb.getCourseName());
                //成绩
                Integer totalScore =Integer.parseInt(studentScoreCollectEntityForWeb.getTotalScore());
                scoreManagerCouseScoreModel.setSingleCourseScore(totalScore);


                    if(CourScoreExitst.containsKey(studentId)==false){

                        CourScoreExitst.put(studentId,"存放"+studentId);

                    }
                    //存 课程名称+成绩
                    scoreManagerCouseScoreModelArrayList.add(scoreManagerCouseScoreModel);

                    //添加 课程名称+成绩
                    singleCourseScoreShowEntity.setCourseScoreList(scoreManagerCouseScoreModelArrayList);


                //map 中是否 存有该 学生信息
                boolean jundgeStudentNum = hashMapTotalScore.containsKey(studentId);

                //用于存放 总分,如果学生唯一标识存在,则说明该学生的牟科成绩已经存放,需要和现成绩相加算总分
                if (jundgeStudentNum) {

                    Integer total = hashMapTotalScore.get(studentId) + totalScore;

                    hashMapTotalScore.put(studentId, total);
                    //获取各科的总分
                    singleCourseScoreShowEntity.setStudentTotalScore(total);

                } else {

                    // 不存在则该学生未在map中,开始他的添加
                    hashMapTotalScore.put(studentId, totalScore);
                    singleCourseScoreShowEntity.setStudentTotalScore(totalScore);

                }

                //存放 拼接的信息
                singleCourseScoreShowEntityMap.put(studentId,singleCourseScoreShowEntity);
            }

        }

        List<SingleCourseScoreShowEntity> singleCourseScoreShowEntityList = new ArrayList<>();

        //遍历实体
        for (Map.Entry<String, SingleCourseScoreShowEntity> stringSingleCourseScoreShowEntityEntry : singleCourseScoreShowEntityMap.entrySet()) {

            singleCourseScoreShowEntityList.add(stringSingleCourseScoreShowEntityEntry.getValue());

        }

        return singleCourseScoreShowEntityList;

    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值