实现成绩排名-

先排序,在排名. 然后排名的时候,进行比较. 如果这一名的成绩和上一名的相同, 那么名次相同, 如果比上一名分数低,那么排名加一

import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
 
class Stu {// 学生类
    private String name;
    private double score;// 成绩
 
    public Stu(String name, double score) {
        this.name = name;
        this.score = score;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
 
//测试类
public class TestDemo {
    public static void main(String[] args) {
        List<Stu> stus = Arrays.asList(new Stu("Tom", 79.5), new Stu("Jack", 52), new Stu("Amdy", 79.5),
                new Stu("Lucy", 68), new Stu("Cherry", 79.5), new Stu("Jerry", 52), new Stu("Sweet", 91),
                new Stu("Solem", 65));
        fun1(stus);
        System.out.println("---------------分割线---------------------");
        fun2(stus);
    }
 
    // 方法一:传统的方法
    public static void fun1(List<Stu> stus) {
        // 按照成绩排序
        stus.sort(new Comparator<Stu>() {
            @Override
            public int compare(Stu s1, Stu s2) {
                return -Double.compare(s1.getScore(), s2.getScore());
            }
        });
        int index = 0;// 排名
        double lastScore = -1;// 最近一次的分
 
        for (int i = 0; i < stus.size(); i++) {
            Stu s = stus.get(i);
            if (Double.compare(lastScore, s.getScore())!=0) { // 如果成绩和上一名的成绩不相同,那么排名+1
                lastScore = s.getScore();
                index++;
            }
            System.out.println("名次:" + index + "\t分数" + s.getScore() + "\t名字" + s.getName());
        }
    }
 
 
    // 方法2:  Stream 来进行分组排序
    public static void fun2(List<Stu> stus) {
        List<Entry<Double, List<Stu>>> list = stus.stream().collect(Collectors.groupingBy(Stu::getScore)).entrySet()
                .stream().sorted((s1, s2) -> -Double.compare(s1.getKey(), s2.getKey())).collect(Collectors.toList());
        int index = 1;
        for (Entry<Double, List<Stu>> entry : list) {
            System.out.print("名次:" + index + "\t分数:" + entry.getKey() + "\t名字");
            entry.getValue().forEach((s) -> System.out.print("  " + s.getName()));
            System.out.println();
            index++;
        }
    }
}
结果
名次:1    分数91.0  名字Sweet
名次:2    分数79.5  名字Tom
名次:2    分数79.5  名字Amdy
名次:2    分数79.5  名字Cherry
名次:3    分数68.0  名字Lucy
名次:4    分数65.0  名字Solem
名次:5    分数52.0  名字Jack
名次:5    分数52.0  名字Jerry
---------------分割线---------------------
名次:1    分数:91.0 名字  Sweet
名次:2    分数:79.5 名字  Tom  Amdy  Cherry
名次:3    分数:68.0 名字  Lucy
名次:4    分数:65.0 名字  Solem
名次:5    分数:52.0 名字  Jack  Jerry

另一种情况

//先排序
Collections.sort(stdGpas, new Comparator<StdGpa>(){
@Override
    public int compare(StdGpa s1, StdGpa s2) {
    return -Double.compare(s1.getGpa(), s2.getGpa());
    }
});
int index = 0;// 排名
int no = 0;//去重
double lastScore = -1;// 最近一次的分
//排明
for (int i = 0; i < stdGpas.size(); i++) {
    StdGpa s = stdGpas.get(i);
        if (Double.compare(lastScore, s.getGpa())!=0) { // 如果成绩和上一名的成绩不相同,那么排名+1
        lastScore = s.getGpa();
        index = index + 1 + no;
        no = 0 ;
    }else{//成绩相同人数加一
        no++;
    }
gpaRank.put(s.getId(), index);
}

//方法二
List<Entry<Double, List<Stu>>> list = stus.stream().collect(Collectors.groupingBy(Stu::getScore)).entrySet()
                .stream().sorted((s1, s2) -> -Double.compare(s1.getKey(), s2.getKey())).collect(Collectors.toList());
        int index = 1;
        int num = 0;
        for (Entry<Double, List<Stu>> entry : list) {
            int grade = num+index;
            System.out.print("名次:" + grade + "\t分数:" + entry.getKey() + "\t名字");
            entry.getValue().forEach((s) -> System.out.print("  " + s.getName()));
            System.out.println();
            num = num + entry.getValue().size();
        }
结果
名次:1    分数91.0  名字Sweet
名次:2    分数79.5  名字Tom
名次:2    分数79.5  名字Amdy
名次:2    分数79.5  名字Cherry
名次:5    分数68.0  名字Lucy
名次:6    分数65.0  名字Solem
名次:7    分数52.0  名字Jack
名次:7    分数52.0  名字Jerry
---------------分割线---------------------
名次:1    分数:91.0 名字  Sweet
名次:2    分数:79.5 名字  Tom  Amdy  Cherry
名次:4    分数:68.0 名字  Lucy
名次:6    分数:65.0 名字  Solem
名次:7    分数:52.0 名字  Jack  Jerry
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值