第一种是分数一样的排名不相同,排名不重复。分数为空的考生不参与排名,排在后面。
第二种是分数一样排名相同,排名重复,但是会把位置占掉。(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