treeset java中的多个覆盖

时间:2016-11-29 16:15:01

标签: java treeset

我有一个包含studentId,name和semesterNo的少数学生的arrayList。有条目具有相同的studentId但学期编号不同。我必须根据studentId从列表中删除重复项,并且输出应该按学期的递增顺序排列。为了删除重复项,我实现了可比较和树集来检查id是否相同。我的问题是如何根据学期编号对树集进行排序。

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());
}

1 个答案:

答案 0 :(得分:1)

首先比较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;
    }

}

这是测试类

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]]