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; } } }我想要把根据成绩排序改为根据学号从大到小排序
时间: 2024-03-13 11:45:58 浏览: 205
要按照学号从大到小排序,你可以将 `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) { Scanner input = new Scanner(System.in); String s = null; HashMap<String, Student> Sites = new HashMap<String, Student>(); while (!"end".equals(s)){ s = input.nextLine(); if (!"end".equals(s)){ String [] sr = s.split(" "); Student student = new Student(sr[1],Integer.parseInt(sr[2])); Sites.put(sr[0],student); } else break; } } } class Student{ private String name; private int scores; public Student(String name, int scores) { this.name = name; this.scores = scores; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScores() { return scores; } public void setScores(int scores) { this.scores = scores; } }将这个按学号排序
要按学号排序,可以使用 TreeMap 来代替 HashMap,因为 TreeMap 是有序的键值对集合,可以按键自然顺序或者指定的比较器进行排序。在这里,我们可以指定一个比较器来按学号进行排序,具体实现如下:
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = null;
TreeMap<String, Student> sites = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
}); // 使用 TreeMap 替换 HashMap,并指定比较器按学号排序
while (!"end".equals(s)) {
s = input.nextLine();
if (!"end".equals(s)) {
String[] sr = s.split(" ");
Student student = new Student(sr[1], Integer.parseInt(sr[2]));
sites.put(sr[0], student);
} else
break;
}
// 遍历 TreeMap,输出学生信息
for (Map.Entry<String, Student> entry : sites.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue().getName() + " " + entry.getValue().getScores());
}
}
}
class Student {
private String name;
private int scores;
public Student(String name, int scores) {
this.name = name;
this.scores = scores;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScores() {
return scores;
}
public void setScores(int scores) {
this.scores = scores;
}
}
```
在这个程序中,我们使用 TreeMap 来替换 HashMap,并指定一个比较器来按学号进行排序。在遍历 TreeMap 时,会按照学号的顺序输出学生信息。
编写一个程序,实现下列要求: (1)创建一个Student类实现Comparable接口,包含以下属性(int)id,(String)name,(int)age,(String)sex。 (2)只提供了有参构造函数Student(int id, String name, int age, String sex)。 (3)重写toString()方法,输出学生的所有信息。 编写一个测试类Demo: (1)在测试类Demo中,创建三个学生,属性分别为 id=1 name=tom age=20 sex=男 id=3 name=fox age=19 sex=男 id=2 name=james age=21 sex=男 (2)创建一个TreeMap<Student,String>集合,令key值为学生对象,value值为学生的姓名,要求得到所有key值,并根据学生的age进行从大到小的排序输出学生信息。
Student类的代码实现如下:
```java
import java.util.Objects;
public class Student implements Comparable<Student> {
private int id;
private String name;
private int age;
private String sex;
public Student(int id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
@Override
public int compareTo(Student o) {
return Integer.compare(this.age, o.age);
}
// 重写 equals 和 hashCode 方法,用于比较两个 Student 对象是否相等
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return id == student.id &&
age == student.age &&
Objects.equals(name, student.name) &&
Objects.equals(sex, student.sex);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age, sex);
}
}
```
Demo类的代码实现如下:
```java
import java.util.TreeMap;
public class Demo {
public static void main(String[] args) {
Student student1 = new Student(1, "tom", 20, "男");
Student student2 = new Student(3, "fox", 19, "男");
Student student3 = new Student(2, "james", 21, "男");
TreeMap<Student, String> map = new TreeMap<>();
map.put(student1, student1.getName());
map.put(student2, student2.getName());
map.put(student3, student3.getName());
for (Student s : map.keySet()) {
System.out.println(s.toString());
}
}
}
```
输出结果为:
```text
Student{id=3, name='fox', age=19, sex='男'}
Student{id=1, name='tom', age=20, sex='男'}
Student{id=2, name='james', age=21, sex='男'}
```
可以看到,学生信息根据年龄从大到小排序输出了。
阅读全文