首页 > 编程语言 >java8 成绩分数排名

java8 成绩分数排名

时间:2022-11-28 18:26:25浏览次数:34  
标签:分数 null name jar rank score Student 成绩 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到:11共11个名次
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

标签:分数,null,name,jar,rank,score,Student,成绩,java8
From: https://www.cnblogs.com/hm201402/p/16932951.html

相关文章

  • 基于Servlet+jsp+mysql开发javaWeb学生管理系统(学生信息、学生选课、学生成绩、学生
    你知道的越多,你不知道的越多点赞再看,养成习惯文章目录​​一、开发背景​​​​二、需求分析​​​​三、开发环境​​​​四、运行效果​​​​五、开发流程​​​​工......
  • noi 1.5 32 求分数叙述列和
    描述有一个分数序列q1/p1,q2/p2,q3/p3,q4/p4,q5/p5,....,其中qi+1=qi+pi,pi+1=qi,p1=1,q1=2。比如这个序列前6项分别是2/1,3/2,5/3,8/5,13/8,21/13。求这个分数......
  • 竞赛成绩一览
    CSP-J20190+100+25+80第一题CE,挂1002=CSP-J2020100+100+0+25感觉自己啥都不会,但没挂分1=CSP-S202150+0+40+0磕T1但没磕出来,菜2=NOIP202150+50+12+0T1在......
  • 输入10个同学的成绩求平均值
    很明显我们要先先创建一个数组再利用循环将10个人的成绩输入进去最后打印平均值#include<stdio.h>#defineN10//这里的作用就是创建了一个值为10的常量。intmain(){floa......
  • 如何用JavaScripte和HTML 实现一整套的考试答题卡和成绩表
    相信在学校的你都有这样的体验,临近考试,要疯狂的“背诵”否则成绩单就要挂零,因为答题卡全部涂抹都是错的。那么毕业多年的你,没有了考试,有没有一丝怀念涂答题卡的时候,有没有......
  • Java8新特性
    Java8NashornJavaScriptJava8新特性Java8新特性Nashorn一个javascript引擎。NashornJavaScriptEngine在Java15已经不可用了。这已经在Java11标记为:@de......
  • 有一个班3个学生,各学四门学科,计算总平均分数以及第n个学生的成绩
    #include<stdio.h>intmain(){ voidaverage(float*p,intn); voidsearch(float(*p)[4],intn); floatscore[3][4]={{65,67,70,60},{80,87,90,81},{90,99,100,98}};......
  • Java8新特性
    Java8新特性Java8(又称为jdk1.8)是Java语言开发的一个主要版本。Oracle公司于2014年3月18日发布Java8,它支持函数式编程,新的JavaScript引擎,新的日期......
  • java8 升级 java11 的一些bug
    1.反射异常有些反射异常,不是自己代码的错而是一些框架调用的时候,所带来的,不好处理。用压制输出的形式,1行为压制,2行为调试模式,输出所有的报错信息。这里用java.base/ja......
  • java8新特性
    什么是函数式(Functional)接口只包含一个抽象方法的接口,称为函数式接口。你可以通过Lambda表达式来创建该接口的对象。(若Lambda表达式抛出一个受检异常(即:非运行时异常),那么......