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

第二种是分数一样排名相同,排名重复,但是会把位置占掉。(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

一起学习,共同进步!

公众号搜索: 果酱桑

java8 成绩分数排名相关推荐

  1. 2021年上海高考成绩分数排名查询,2021年上海高考成绩排名及一分一段表

    一分一段表是很重要的填报志愿的依据,也是各大高校划自己分数线的一部分依据.一分一段表是从高到低的排名,考生根据成绩对照一分一段表,就可以知道自己在全省的排名,再参照各类学校各批次招生计划数,确定自己该 ...

  2. 2021北京市高考成绩排名查询,北京2021高考分数排名换算

    6选3模式简介 从2020年起,北京市统一高考科目调整为语文.数学.外语3门,不分文理科,每门科目满分150分,总分450分.物理.化学.生物.历史.地理.政治6门科目选择3门进行高考. 即" ...

  3. 浙江大学计算机云南分数线,985大学在云南录取分数排名,想上清北、浙大、南大至少这个成绩...

    985大学在云南录取分数排名,想上清北.浙大.南大至少这个成绩 今年的志愿填报已经接近尾声,现在还在为填志愿而烦恼的大多为确定需要调剂的考生,而其余的考生正在安心等待高校录取的结果.云南省位于我国的西 ...

  4. 好分数网查成绩分数查排名服务平台_好分数免费查年级排名

    好分数在线查年段排名,好分数在线查班级排名,好分数免费查询同学成绩 分数API是一个公益性的在线免费查排名软件 由作者一木独立开发 如果您方便的话可以下载软件后投币来支持我们 以维持服务器的正常支出, ...

  5. 云南省2021高考成绩排名查询,2020年云南高考成绩位次排名及一分一段表查询

    2020年云南高考成绩公布后,大家是不是很想知道,自己的成绩在哪个层次,全省能够排第几?云南高考一分一段表将给你明确的答案,并且能够让你直观看到:高考1分干掉千人的真相!2020年<云南高考成绩 ...

  6. 《实时控制软件开发》学生四次作业成绩总排名

    学生作业部分的成绩占总成绩(100分制)76分,其中,个人作业占48分,团队Project占28分. 学生个人作业得分 映射规则,由学生三次个人作业平均分占每次作业总分(10分)的比率,映射到个人作业 ...

  7. 怎么利用计算机为学生成绩进行排名,使用excel为学生成绩排序的方法和步骤

    在下面表格中学号和姓名等是用计算机录入并打印出来,老师只是手写填上考试成绩和排名,然后让打字员录入表格中.当然我也看到或听说过其他学校的学生成绩统计分数排名表.它是对学生成绩进行升序或降序的排列表.这 ...

  8. 2021高考成绩省排名查询,【重磅整理】2021全国各地高考预测分数线出炉,这样估分可以估算全省排名...

    今天特别整理2021年各地高考预测分数线,大家估完分后可以参考一下哦! 2021年各地高考预测分数线(非官方公布,仅供参考) 特别说明:本文整理的高考预测分数线为不完全整理,由各地网友或老师预测,仅供 ...

  9. 2021高考成绩省内排名查询,云南高考排名查询方法 2021年云南高考成绩位次全省排名查询...

    摘要:云南高考成绩排名查询方法,云南省高考主要是通过一段考试表,查询自己的排名,计算全省的绩效排名.云南高考状元表中每一分数线有多少考生.分数线以上的考生有多少进行详细统计. 云南高考成绩排名查询方法 ...

  10. 河南2021年高考成绩位次查询,河南高考成绩位次排名查询2020,河南高考一分一段表...

    每年河南高考成绩出来后,很多考生都不知道如何查询高考成绩位次排名,本文教大家如果查询河南高考个人成绩位次排名的相关知识,首先我们要知道什么是位次:位次是按照文化课总分从高到低进行排列的,总分相同时,当 ...

最新文章

  1. 别被布线“老思想”拌倒
  2. linux zookeeper安装并设置开机自启
  3. mybatis 动态 SQL
  4. [react-router] React-Router 4中<Router>组件有几种类型?
  5. 访问云服务器储存的mp4_访问云服务器储存的mp4
  6. 【中文分词】结构化感知器SP
  7. 六石管理学:流程是为工作服务的
  8. 移动互联软件技术与实践demo
  9. java和python爬虫那个好_java和python在爬虫方面的优势和劣势是什么?
  10. A. 运维体系 --- SLA理论体系
  11. 1分钟搞定 OneNote自己账号扩容到15G永久免费空间
  12. 施德来:有赞电商小程序的实践
  13. 格物、致知、正心、诚意、修身、齐家、治国、平天下
  14. 第八天 字符串 流程控制(二)
  15. python儿童编程培训班-重庆少儿Python编程培训班
  16. C语言|博客作业06
  17. Echarts 生成地图html
  18. 计算机统考沪闽,16-17计算机408统考真题及详解(10页)-原创力文档
  19. 深入小程序云开发之云函数 第三部分 云函数经验谈
  20. NBIOT 模块连接阿里云(1)

热门文章

  1. Ping命令进行网络检测
  2. 135. 精读《极客公园 IFX - 上》
  3. Compile、Make和Build的区别
  4. java计算机毕业设计晨光文具店进销存系统设计与开发源码+数据库+系统+lw文档+部署
  5. tomcat发布网站的三种方式
  6. python怎么表达我喜欢你的句子_关于我喜欢你的优美句子
  7. python——字符串练习:句子反转(小米笔试题)
  8. 安装pywifi的坑
  9. PostgreSQL 15.0下载与安装详细保姆教程
  10. 微服务api网关_微服务设计api网关模式