https://zhidao.baidu.com/question/1900171286360976020.html?qq-pf-to=pcqq.c2c
思路: 排序肯定还是要排的, 按照Java成绩来进行排练. 然后排名的时候,进行比较. 如果这一名的成绩和上一名的相同, 那么名次相同, 如果比上一名分数低,那么排名加一.
可以使用传统的,集合排序,输出. 也可以使用java8新提供的Stream API进行操作
参考代码如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
import java.util.*;import java.util.Map.Entry;import java.util.stream.Collectors;class Stu {// 学生类 private String name; private double score;// 成绩 public Stu(String name, double score) { this.name = name; this.score = score; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; }}//测试类public class TestDemo { public static void main(String[] args) { List<Stu> stus = Arrays.asList(new Stu("Tom", 79.5), new Stu("Jack", 52), new Stu("Amdy", 79.5), new Stu("Lucy", 68), new Stu("Cherry", 79.5), new Stu("Jerry", 52), new Stu("Sweet", 91), new Stu("Solem", 65)); fun1(stus); System.out.println("---------------分割线---------------------"); fun2(stus); } // 方法一:传统的方法 public static void fun1(List<Stu> stus) { // 按照成绩排序 stus.sort(new Comparator<Stu>() { @Override public int compare(Stu s1, Stu s2) { return -Double.compare(s1.getScore(), s2.getScore()); } }); int index = 0;// 排名 double lastScore = -1;// 最近一次的分 for (int i = 0; i < stus.size(); i++) { Stu s = stus.get(i); if (Double.compare(lastScore, s.getScore())!=0) { // 如果成绩和上一名的成绩不相同,那么排名+1 lastScore = s.getScore(); index++; } System.out.println("名次:" + index + " 分数" + s.getScore() + " 名字" + s.getName()); } } // 方法2: Java8开始支持的Lambada表达式配合 Stream API 来进行分组排序 public static void fun2(List<Stu> stus) { List<Entry<Double, List<Stu>>> list = stus.stream().collect(Collectors.groupingBy(Stu::getScore)).entrySet() .stream().sorted((s1, s2) -> -Double.compare(s1.getKey(), s2.getKey())).collect(Collectors.toList()); int index = 1; for (Entry<Double, List<Stu>> entry : list) { System.out.print("名次:" + index + " 分数:" + entry.getKey() + " 名字"); entry.getValue().forEach((s) -> System.out.print(" " + s.getName())); System.out.println(); index++; } }} |
输出结果
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
名次:1 分数91.0 名字Sweet名次:2 分数79.5 名字Tom名次:2 分数79.5 名字Amdy名次:2 分数79.5 名字Cherry名次:3 分数68.0 名字Lucy名次:4 分数65.0 名字Solem名次:5 分数52.0 名字Jack名次:5 分数52.0 名字Jerry名次:1 分数:91.0 名字 Sweet名次:2 分数:79.5 名字 Tom Amdy Cherry名次:3 分数:68.0 名字 Lucy名次:4 分数:65.0 名字 Solem名次:5 分数:52.0 名字 Jack Jerry---------------分割线---------------------名次:1 分数:91.0 名字 Sweet名次:2 分数:79.5 名字 Tom Amdy Cherry名次:3 分数:68.0 名字 Lucy名次:4 分数:65.0 名字 Solem名次:5 分数:52.0 名字 Jack Jerry |
另外一种情况,相同并列,不同跳过----
Collections.sort(stdGpas, new Comparator<StdGpa>(){
@Override
public int compare(StdGpa s1, StdGpa s2) {
return -Double.compare(s1.getGpa(), s2.getGpa());
}
});
int index = 0;// 排名
int no = 0;//去重
double lastScore = -1;// 最近一次的分
for (int i = 0; i < stdGpas.size(); i++) {
StdGpa s = stdGpas.get(i);
if (Double.compare(lastScore, s.getGpa())!=0) { // 如果成绩和上一名的成绩不相同,那么排名+1
lastScore = s.getGpa();
index = index + 1 + no;
no = 0 ;
}else{
no++;
}
gpaRank.put(s.getId(), index);
}
