第一种是分数一样的排名不相同,排名不重复。分数为空的考生不参与排名,排在后面。
第二种是分数一样排名相同,排名重复,但是会把位置占掉。(eg:1,2,2,2,2,6,7这种排名相同的情况)分数为空的考生不参与排名,排在后面。
package com.gaodun.test;
import com.google.common.collect.Lists;
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 {
String name;
Integer score;
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)
);
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++) {
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("#################################### 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());
}
}
结果打印:
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)]
3.为空的放在最后(分数相同排名相同)
3.统计排名人数:19
3.排名后:[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)]
Process finished with exit code 0