树集中的多个覆盖 java

multiple overrides in treeset java

我有一个 ArrayList 包含少数学生的 studentIdnamesemesterNo。有相同 studentId 但学期编号不同的条目。我必须根据 studentId 从列表中删除重复项,并且输出应该按学期的递增顺序排列。为了删除重复项,我实施了可比较和 TreeSet 来检查 id 是否相同。

我的问题是如何根据学期编号对 TreeSet 进行排序。

public class Student implements Comparable<Student>{


private Integer studentId;
private String studentName;
private Integer studentSemester;


@Override
public int compareTo(Student o) {
    return o.getStudentId().compareTo(this.getStudentId());
}

先比较 studentId 再比较 studentSemester 的

public class Student implements Comparable<Student> {

    private int studentId;
    private String studentName;
    private int studentSemester;

    public Student(int sId, String sName, int sSemester) {
        this.studentId = sId;
        this.studentName = sName;
        this.studentSemester = sSemester;
    }

    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public int getStudentSemester() {
        return studentSemester;
    }

    public void setStudentSemester(int studentSemester) {
        this.studentSemester = studentSemester;
    }

    @Override
    public String toString() {
        return "[" + studentId + ", " + studentName + ", " + studentSemester + "]";
    }

    @Override
    public int compareTo(Student that) {
        if(this.getStudentId() == that.getStudentId())
            return 0;
        if(this.getStudentSemester() > that.getStudentSemester())
            return 1;
        if(this.getStudentSemester() < that.getStudentSemester())
            return -1;
        return 0;
    }

}

这是测试class

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

public class MultipleOverrides {

    public static void main(String args[]) {

        Student s1 = new Student(1, "A", 1);
        Student s2 = new Student(1, "A", 2);
        Student s3 = new Student(1, "A", 3);
        Student s4 = new Student(2, "B", 1);
        Student s5 = new Student(2, "B", 2);
        Student s6 = new Student(2, "B", 3);
        Student s7 = new Student(3, "C", 1);
        Student s8 = new Student(3, "C", 2);
        Student s9 = new Student(3, "C", 3);

        List<Student> students = new ArrayList<>();
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
        students.add(s5);
        students.add(s6);
        students.add(s7);
        students.add(s8);
        students.add(s9);

        System.out.println("All students before sorting : " + students);

        TreeSet<Student> sortedStudents = new TreeSet<>();
        sortedStudents.addAll(students);
        System.out.println("All students after sorting : " + sortedStudents);

    }

}

输出:

排序前的所有学生:[[1, A, 1], [1, A, 2], [1, A, 3], [2, B, 1], [2, B, 2] , [2, B, 3], [3, C, 1], [3, C, 2], [3, C, 3]]

排序后的所有学生:[[1, A, 1], [2, B, 2], [3, C, 3]]