java做成绩排名表,如何实现同分同名次

https://zhidao.baidu.com/question/1900171286360976020.html?qq-pf-to=pcqq.c2c

 

思路: 排序肯定还是要排的, 按照Java成绩来进行排练. 然后排名的时候,进行比较. 如果这一名的成绩和上一名的相同, 那么名次相同, 如果比上一名分数低,那么排名加一.

可以使用传统的,集合排序,输出. 也可以使用java8新提供的Stream API进行操作

参考代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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: Java8开始支持的Lambada表达式配合 Stream API 来进行分组排序
    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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
名次: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
---------------分割线---------------------
名次: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);
}

 

 

posted @   Effect  阅读(11203)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示