import java.util.*; public class Main { public static void main(String[] args) { TreeMap<String, Student> map = new TreeMap<String,Student>(); Scanner sc = new Scanner(System.in); while(true){ String ID = sc.next(); if(ID.equals("end")){ break; } String name = sc.next(); int score = sc.nextInt(); Student student = new Student(ID,name,score); map.put(ID,student); } Set<String> keyset = new TreeSet<>(); keyset = map.keySet(); for(String key:keyset){ Student student = map.get(key); System.out.println(student.getID() + " " + student.getName() + " " + student.getScore()); } } } class Student{ private String ID;//学号 private String name;//姓名 private int score;//成绩 public Student() { } public Student(String ID, String name, int score) { this.ID = ID; this.name = name; this.score = score; } public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }我这样的代码要改成由从小到大变为由大到小排列,应该怎么处理
时间: 2024-02-14 19:12:03 浏览: 75
java代码-import java.util.Scanner;
可以通过实现 Comparator 接口,并重写 compare 方法来实现 TreeMap 的排序方式的改变。具体的做法是,定义一个新的类 MyComparator 实现 Comparator 接口,在 compare 方法中将两个 Student 对象的成绩 score 进行比较,选择大的那个进行排序。然后在 TreeMap 的构造函数中传入 MyComparator 对象即可。
下面是修改后的代码:
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
TreeMap<String, Student> map = new TreeMap<String, Student>(new MyComparator());
Scanner sc = new Scanner(System.in);
while (true) {
String ID = sc.next();
if (ID.equals("end")) {
break;
}
String name = sc.next();
int score = sc.nextInt();
Student student = new Student(ID, name, score);
map.put(ID, student);
}
Set<String> keyset = new TreeSet<>();
keyset = map.keySet();
for (String key : keyset) {
Student student = map.get(key);
System.out.println(student.getID() + " " + student.getName() + " " + student.getScore());
}
}
}
class Student {
private String ID;//学号
private String name;//姓名
private int score;//成绩
public Student() {
}
public Student(String ID, String name, int score) {
this.ID = ID;
this.name = name;
this.score = score;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
class MyComparator implements Comparator<String> {
TreeMap<String, Student> map = new TreeMap<String, Student>();
public MyComparator() {
}
public MyComparator(TreeMap<String, Student> map) {
this.map = map;
}
@Override
public int compare(String o1, String o2) {
Student s1 = map.get(o1);
Student s2 = map.get(o2);
if (s1.getScore() < s2.getScore()) {
return 1;
} else if (s1.getScore() > s2.getScore()) {
return -1;
} else {
return 0;
}
}
}
```
在 MyComparator 类中,我们将 TreeMap 对象作为成员变量传入,这样在 compare 方法中就可以通过 map.get() 方法获取到相应的 Student 对象,进而进行比较。通过此方法,就可以实现由大到小的排序方式。
阅读全文