上次写的代码(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;
}