TreeMap map = new TreeMap(new Comparator() { @Override public int compare(Object o1, Object o2) { String k1 = (String) o1; String k2 = (String) o2; return k2.compareTo(k1); }});
时间: 2024-04-23 16:21:46 浏览: 90
这段代码创建了一个TreeMap实例,其中的键按照字符串倒序排序。这里使用了一个匿名内部类实现了Comparator接口的compare方法来定义键的比较方式。在compare方法中,先将两个参数o1和o2强制转换成字符串类型的键k1和k2,然后通过k2.compareTo(k1)来比较它们的大小,返回一个整数表示它们的大小关系。如果k2大于k1,则返回正整数;如果k2等于k1,则返回0;如果k2小于k1,则返回负整数。这个比较器将被用于TreeMap的构造方法中,以便保证插入到TreeMap中的键按照指定的排序方式进行排列。
相关问题
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; } } }我想要把根据成绩排序改为根据学号从大到小排序
要按照学号从大到小排序,你可以将 `TreeMap` 的泛型参数中 `String` 改为 `Integer`,这样可以将学号转换为整数类型进行比较。同时,你也需要修改 `MyComparator` 中的 `compare` 方法,将比较逻辑改为根据学号从大到小排序,例如:
```java
class MyComparator implements Comparator<Integer> {
TreeMap<Integer, Student> map;
public MyComparator(TreeMap<Integer, Student> map) {
this.map = map;
}
@Override
public int compare(Integer o1, Integer o2) {
Student s1 = map.get(o1);
Student s2 = map.get(o2);
return o2.compareTo(o1);
}
}
```
在 `compare` 方法中,我们首先获取到要比较的两个学生对象 `s1` 和 `s2`,然后通过比较它们的学号大小来决定它们的顺序。由于要按照学号从大到小排序,因此我们将 `o2` 和 `o1` 进行比较,这样就可以实现根据学号从大到小排序的功能了。
能不能请你帮我完成最后的按学号从大到小排序,这是我的代码按学号从大到小排序 import java.util.*; public class Main { public static void main(String[] args) { TreeMap<String, Student> map = new TreeMap<String,Student>(new MyCompareTor()); 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 MyCompareTor implements Comparator<String>{ TreeMap<String,Student> map ; public MyCompareTor() { } public MyCompareTor(TreeMap<String, Student> map) { this.map = map; } @Override public int compare(String s1, String s2) { Student student1 = map.get(s1); Student student2 = map.get(s2); int i; int ID1 = Integer.parseInt(student1.getID()); int ID2 = Integer.parseInt(student2.getID()); if(ID1 > ID2){ } } }
当您在比较器 MyCompareTor 中实现 compare 方法时,您可以按照以下方式进行比较,以使学号从大到小排序:
```
class MyCompareTor implements Comparator<String> {
TreeMap<String, Student> map;
public MyCompareTor() {
}
public MyCompareTor(TreeMap<String, Student> map) {
this.map = map;
}
@Override
public int compare(String s1, String s2) {
Student student1 = map.get(s1);
Student student2 = map.get(s2);
int ID1 = Integer.parseInt(student1.getID());
int ID2 = Integer.parseInt(student2.getID());
// 按学号从大到小排序
return ID2 - ID1;
}
}
```
这样,您得到的结果将按照学号从大到小排序。
阅读全文