java8 成绩分数排名

第一种是分数一样的排名不相同,排名不重复。分数为空的考生不参与排名,排在后面。

第二种是分数一样排名相同,排名重复,但是会把位置占掉。(eg:1,2,2,2,2,6,7这种排名相同的情况)分数为空的考生不参与排名,排在后面。

package com.gaodun.test;
 
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Data;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @Title: 测试考生信息列表排名
 * @Author: ken
 * @Description:
 * @Date: 2021/7/21  9:32
 **/
@Data
@AllArgsConstructor
class Student {
    private String name;
    private Integer score;
    private Integer rank;
}
 
public class TestRank {
    public static void main(String[] args) {
        List<Student> studentList = Arrays.asList(
                new Student("ken1", 10, null),
                new Student("ken2", 20, null),
                new Student("ken3", 30, null),
                new Student("ken4", 40, null),
                new Student("ken5", 50, null),
                new Student("ken6", 60, null),
                new Student("ken7", 70, null),
                new Student("ken8", 80, null),
                new Student("ken9", 90, null),
                new Student("ken10", 100, null),
                new Student("ken11", 100, null),
                new Student("ken12", 100, null),
                new Student("ken13", 100, null),
                new Student("ken14", 990, null),
                new Student("ken15", null, null),
                new Student("ken16", null, null),
                new Student("ken17", null, null),
                new Student("ken18", null, null),
                new Student("ken19", null, null)
        );
        //Arrays.asList() 初始化的list 不能修改list 结构,但是可以修改里面对象内容,下面写法会报错
        //studentList.add(new Student("ken20", 1, null));
        
        System.out.println("#################################### 1.过滤掉分数为空的学生进行排名(排名不重复) ####################################");
        System.out.println("排名前:" + studentList.toString());
        //过滤掉分数为空值的学生进行排名
        List<Student> studentSortList = studentList.stream().filter(a -> Objects.nonNull(a.getScore())).sorted(Comparator.comparing(Student::getScore)
                .reversed()).collect(Collectors.toList());
/*      Map<String, Integer> userRankMap = Maps.newConcurrentMap();
        for (int i = 0; i < studentSortList.size(); i++) {
            userRankMap.put(studentSortList.get(i).getName(), i + 1);
        }
        for (Map.Entry<String, Integer> entry : userRankMap.entrySet()) {
            System.out.println(entry.getKey() + "--->" + entry.getValue());
        }*/
        /*studentSortList.forEach(a ->{
            System.out.println();
        });*/
        Map<String, Integer> userRankMap = Maps.newConcurrentMap();
        for (int i = 0; i < studentSortList.size(); i++) {
            studentSortList.get(i).setRank(i + 1);
        }
        List<Student> studentScoreNullList = studentList.stream().filter(a -> Objects.isNull(a.getScore())).collect(Collectors.toList());
        List<Student> studentNotpeatScoreNullList = studentSortList;
        //先统计有分数学员的人数,再把没有分数的考生加进去
        studentNotpeatScoreNullList.addAll(studentScoreNullList);
        System.out.println("1.统计排名人数:" + studentNotpeatScoreNullList.size());
        System.out.println("1.排名后:" + studentNotpeatScoreNullList.toString());
 
        System.out.println("#################################### 1.1第一种优化 ####################################");
        List<Student> optimizeSortList = studentList;
        optimizeSortList.sort(Comparator.comparing(Student::getScore, Comparator.nullsFirst(Integer::compareTo)).reversed());
        for (int i = 0; i < optimizeSortList.size(); i++) {
            optimizeSortList.get(i).setRank(i + 1);
        }
        System.out.println("1.1统计排名人数:" + optimizeSortList.size());
        System.out.println("1.1排名后:" + optimizeSortList.toString());
 
        System.out.println("#################################### 2.过滤掉分数为空的学生(分数相同排名相同) ####################################");
        System.out.println("2.排名前人数:" + studentList.size());
        System.out.println("2.排名前:" + studentList);
        List<Student> lists = studentList.stream().filter(a -> Objects.nonNull(a.getScore())).sorted((s1, s2) -> -Integer.compare(s1.getScore(), s2.getScore())).collect(Collectors.toList());
        List<Map.Entry<Integer, List<Student>>> list = lists.stream()
                .collect(Collectors.groupingBy(Student::getScore)).entrySet()
                .stream().sorted((s1, s2) -> -Long.compare(s1.getKey(), s2.getKey())).collect(Collectors.toList());
        int index = 1;
        for (Map.Entry<Integer, List<Student>> entry : list) {
            for (Student student : entry.getValue()) {
                student.setRank(index);
            }
            index = index + entry.getValue().size();
        }
        System.out.println("2.统计排名人数(分数不为null):" + lists.size());
        System.out.println("2.名次从1到:" + list.size() + "共" + list.size() + "个名次");
        System.out.println("2.排名后:" + list);
 
        System.out.println("#################################### 3.为空的放在最后(分数相同排名相同) ####################################");
        List<Student> finalList = studentList.stream().sorted(Comparator.comparing(Student::getScore, Comparator.nullsFirst(Integer::compareTo))
                .reversed()).collect(Collectors.toList());
        System.out.println("3.统计排名人数:" + finalList.size());
        System.out.println("3.排名后:" + finalList.toString());
    }

"C:\Program Files\Java\jdk1.8.0_231\bin\java.exe" "-javaagent:E:\program files\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=62163:E:\program files\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_231\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\rt.jar;E:\NewWorld\work\workspace\classes\production\QianQian;E:\NewWorld\repository\org\apache\httpcomponents\httpclient\4.5.12\httpclient-4.5.12.jar;E:\NewWorld\repository\org\apache\httpcomponents\httpcore\4.4.13\httpcore-4.4.13.jar;E:\NewWorld\repository\org\projectlombok\lombok\1.18.12\lombok-1.18.12.jar;E:\NewWorld\repository\com\google\guava\guava\28.2-android\guava-28.2-android.jar;E:\NewWorld\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;E:\NewWorld\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar" com.gaodun.test.TestRank
#################################### 1.过滤掉分数为空的学生进行排名(排名不重复) ####################################
排名前:[Student(name=ken1, score=10, rank=null), Student(name=ken2, score=20, rank=null), Student(name=ken3, score=30, rank=null), Student(name=ken4, score=40, rank=null), Student(name=ken5, score=50, rank=null), Student(name=ken6, score=60, rank=null), Student(name=ken7, score=70, rank=null), Student(name=ken8, score=80, rank=null), Student(name=ken9, score=90, rank=null), Student(name=ken10, score=100, rank=null), Student(name=ken11, score=100, rank=null), Student(name=ken12, score=100, rank=null), Student(name=ken13, score=100, rank=null), Student(name=ken14, score=990, rank=null), Student(name=ken15, score=null, rank=null), Student(name=ken16, score=null, rank=null), Student(name=ken17, score=null, rank=null), Student(name=ken18, score=null, rank=null), Student(name=ken19, score=null, rank=null)]
1.统计排名人数:19
1.排名后:[Student(name=ken14, score=990, rank=1), Student(name=ken10, score=100, rank=2), Student(name=ken11, score=100, rank=3), Student(name=ken12, score=100, rank=4), Student(name=ken13, score=100, rank=5), Student(name=ken9, score=90, rank=6), Student(name=ken8, score=80, rank=7), Student(name=ken7, score=70, rank=8), Student(name=ken6, score=60, rank=9), Student(name=ken5, score=50, rank=10), Student(name=ken4, score=40, rank=11), Student(name=ken3, score=30, rank=12), Student(name=ken2, score=20, rank=13), Student(name=ken1, score=10, rank=14), Student(name=ken15, score=null, rank=null), Student(name=ken16, score=null, rank=null), Student(name=ken17, score=null, rank=null), Student(name=ken18, score=null, rank=null), Student(name=ken19, score=null, rank=null)]
#################################### 1.1第一种优化 ####################################
1.1统计排名人数:19
1.1排名后:[Student(name=ken14, score=990, rank=1), Student(name=ken10, score=100, rank=2), Student(name=ken11, score=100, rank=3), Student(name=ken12, score=100, rank=4), Student(name=ken13, score=100, rank=5), Student(name=ken9, score=90, rank=6), Student(name=ken8, score=80, rank=7), Student(name=ken7, score=70, rank=8), Student(name=ken6, score=60, rank=9), Student(name=ken5, score=50, rank=10), Student(name=ken4, score=40, rank=11), Student(name=ken3, score=30, rank=12), Student(name=ken2, score=20, rank=13), Student(name=ken1, score=10, rank=14), Student(name=ken15, score=null, rank=15), Student(name=ken16, score=null, rank=16), Student(name=ken17, score=null, rank=17), Student(name=ken18, score=null, rank=18), Student(name=ken19, score=null, rank=19)]
#################################### 2.过滤掉分数为空的学生(分数相同排名相同) ####################################
2.排名前人数:19
2.排名前:[Student(name=ken14, score=990, rank=1), Student(name=ken10, score=100, rank=2), Student(name=ken11, score=100, rank=3), Student(name=ken12, score=100, rank=4), Student(name=ken13, score=100, rank=5), Student(name=ken9, score=90, rank=6), Student(name=ken8, score=80, rank=7), Student(name=ken7, score=70, rank=8), Student(name=ken6, score=60, rank=9), Student(name=ken5, score=50, rank=10), Student(name=ken4, score=40, rank=11), Student(name=ken3, score=30, rank=12), Student(name=ken2, score=20, rank=13), Student(name=ken1, score=10, rank=14), Student(name=ken15, score=null, rank=15), Student(name=ken16, score=null, rank=16), Student(name=ken17, score=null, rank=17), Student(name=ken18, score=null, rank=18), Student(name=ken19, score=null, rank=19)]
2.统计排名人数(分数不为null:14
2.名次从1:1111个名次
2.排名后:[990=[Student(name=ken14, score=990, rank=1)], 100=[Student(name=ken10, score=100, rank=2), Student(name=ken11, score=100, rank=2), Student(name=ken12, score=100, rank=2), Student(name=ken13, score=100, rank=2)], 90=[Student(name=ken9, score=90, rank=6)], 80=[Student(name=ken8, score=80, rank=7)], 70=[Student(name=ken7, score=70, rank=8)], 60=[Student(name=ken6, score=60, rank=9)], 50=[Student(name=ken5, score=50, rank=10)], 40=[Student(name=ken4, score=40, rank=11)], 30=[Student(name=ken3, score=30, rank=12)], 20=[Student(name=ken2, score=20, rank=13)], 10=[Student(name=ken1, score=10, rank=14)]]
#################################### 3.为空的放在最后(分数相同排名相同) ####################################
3.统计排名人数:19
3.排名后:[Student(name=ken14, score=990, rank=1), Student(name=ken10, score=100, rank=2), Student(name=ken11, score=100, rank=2), Student(name=ken12, score=100, rank=2), Student(name=ken13, score=100, rank=2), Student(name=ken9, score=90, rank=6), Student(name=ken8, score=80, rank=7), Student(name=ken7, score=70, rank=8), Student(name=ken6, score=60, rank=9), Student(name=ken5, score=50, rank=10), Student(name=ken4, score=40, rank=11), Student(name=ken3, score=30, rank=12), Student(name=ken2, score=20, rank=13), Student(name=ken1, score=10, rank=14), Student(name=ken15, score=null, rank=15), Student(name=ken16, score=null, rank=16), Student(name=ken17, score=null, rank=17), Student(name=ken18, score=null, rank=18), Student(name=ken19, score=null, rank=19)]
 
Process finished with exit code 0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个可能的 Java 成绩排名程序,它将从命令行读取每个学生的分数和姓名,然后按分数从高到低排名并打印输出排名结果: ```java import java.util.*; public class ScoreRank { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Map<String, Integer> scores = new HashMap<>(); System.out.print("请输入学生数目:"); int num = scanner.nextInt(); scanner.nextLine(); // 消耗掉换行符 for (int i = 1; i <= num; i++) { System.out.print("请输入第" + i + "个学生的姓名和分数,中间用空格隔开:"); String[] parts = scanner.nextLine().split(" "); String name = parts[0]; int score = Integer.parseInt(parts[1]); scores.put(name, score); } List<Map.Entry<String, Integer>> sortedEntries = new ArrayList<>(scores.entrySet()); Collections.sort(sortedEntries, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) { return e2.getValue().compareTo(e1.getValue()); } }); System.out.println("成绩排名:"); int rank = 1; for (Map.Entry<String, Integer> entry : sortedEntries) { System.out.println(rank + ". " + entry.getKey() + ": " + entry.getValue()); rank++; } } } ``` 这个程序使用了 Java 的集合框架中的 Map 和 List,并且定义了一个匿名内部类作为比较器来进行排序。在打印排名结果时,程序使用了一个变量来记录当前排名,每次输出一个学生的信息时就将排名加一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值