求学生排名

        public static void main(String[] args) {

            addStu(1,1,120d);
            addStu(1,2,120d);
            addStu(1,3,119d);
            addStu(2,4,120d);
            addStu(3,5,121d);
            int rank = 1;
            Double lastScore = null;
            Integer lastClass = null;
            int count = 1;

            HashSet<Integer> classSet=new HashSet<Integer>();
            for (int i = 0; i < stuScoreListNew.size(); i++) {
                HashMap<String, Object> hashMap = stuScoreListNew.get(i);
                Double allScore = (Double) hashMap.get("allScore");
                Integer classId = (Integer) hashMap.get("classId");
                if(classId==null){
                    continue;
                }
                String userId = hashMap.get("userId") + "";
                if (lastClass == null) {
                    lastClass = classId;
                    if (lastScore == null) {
                        lastScore = allScore;
                    } else {
                        if (allScore.compareTo(lastScore) == 0) {
                            count += 1;
                        } else {
                            rank += count;
                            count = 1;
                        }
                        lastScore = allScore;
                    }
                } else {
                    classSet.add(lastClass);
                    if (lastClass.equals(classId)) {
                        if (allScore.compareTo(lastScore) == 0) {
                            count += 1;
                        } else {
                            rank += count;
                            count = 1;
                        }
                        lastScore = allScore;
                    } else {
                        classSet.add(classId);
                        lastClass = null;
                        count = 1;
                        rank = 1;
                        lastScore = allScore;
                    }
                }

                System.out.println(userId+":"+rank);
            }
        }

        private static void addStu(Integer classId,Integer userId,Double allScore){
            HashMap<String,Object> stu=new HashMap<>();
            stu.put("classId",classId);
            stu.put("userId",userId);
            stu.put("allScore",allScore);
            stuScoreListNew.add(stu);
        }

目前输出:
1:1
2:1
3:3
4:1
5:2

明显userId为5的学生班级排名不正确,求修正后的代码

private static  List<HashMap<String,Object>> stuScoreListNew=new ArrayList<>();
    public static void main(String[] args) {

        addStu(1,1,120d);
        addStu(1,2,119d);
        addStu(1,3,119d);
        addStu(1,8,118d);
        addStu(2,4,120d);
        addStu(3,5,121d);
        addStu(3,6,120d);
        addStu(3,7,120d);
        int rank = 1;
        Double lastScore = null;
        Integer lastClass = null;
        int count = 1;
        for (int i = 0; i < stuScoreListNew.size(); i++) {
            HashMap<String, Object> hashMap = stuScoreListNew.get(i);
            Double allScore = (Double) hashMap.get("allScore");
            Integer classId = (Integer) hashMap.get("classId");
            if(classId==null){
                continue;
            }
            String userId = hashMap.get("userId") + "";
            if (lastClass == null) {
                lastClass = classId;
                lastScore = allScore;
            } else {
                if (lastClass.equals(classId)) {
                    if (allScore.compareTo(lastScore) == 0) {
                        count += 1;
                    } else {
                        rank += count;
                        count = 1;
                    }
                    lastScore = allScore;
                } else {
                    lastClass = classId;
                    count = 1;
                    rank = 1;
                    lastScore = allScore;
                }
            }

            System.out.println(userId+":"+rank);
        }
    }

    private static void addStu(Integer classId,Integer userId,Double allScore){
        HashMap<String,Object> stu=new HashMap<>();
        stu.put("classId",classId);
        stu.put("userId",userId);
        stu.put("allScore",allScore);
        stuScoreListNew.add(stu);
    }

已修复