1

我有 HashSet,其中包含学生、教授和部门的对象列表。它需要检查总分最高的学生和总分最高的教授。示例代码表示像

  class Student{
    public String Name;
    public int Age;
    public int TotalMarks;
}
class Professor{
    public String Name;
    public int Age;
    public Student Student_Assigned;
}

class Department{
    Set<Professor> collectionDept = new HashSet<Professor>();

    public Student getStudentWithHigestMarks(){
        return null;
    }
}

我怎样才能使用java找到这个?

4

4 回答 4

2

实施Comparable和排序/操作您的Collection.

例如在您的主要代码中:

Student bad = new Student("bad");
bad.setMarks(2);
Student meh = new Student("meh");
meh.setMarks(5);
Student good = new Student("good");
good.setMarks(10);
Student otherGood = new Student("otherGood");
otherGood.setMarks(10);

// initializes a Set of students
Set<Student> students = new HashSet<Student>();
// adds the students
students.add(meh);
students.add(bad);
students.add(good);
students.add(otherGood);
// prints the "best student"
System.out.println(Collections.max(students).getName());
// initializing Set of best students
List<Student> bestStudents = new ArrayList<Student>();
// finding best mark
int bestMark = Collections.max(students).getMarks();
// adding to best students if has best mark
for (Student s: students) {
    if (s.getMarks() == bestMark) {
        bestStudents.add(s);
    }
}
// printing best students
for (Student s: bestStudents) {
    System.out.println(s.getName());
}

输出:

good
good
otherGood

...这是您Student班级的草稿:

public class Student implements Comparable<Student> {
    // we use encapsulation and set the fields' access to private
    private String name;
    private int age;
    private int marks;
    // we use encapsulation and have setters/getters/constructor access for the private fields
    public Student(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public int getMarks() {
        return marks;
    }
    public void setMarks(int marks) {
        this.marks = marks;
    }
    // TODO other setters/getters
    // here we implement the compareTo method and decide which int to return according to the "marks" field
    @Override
    public int compareTo(Student otherStudent) {
        if (marks < otherStudent.getMarks()) {
            return -1;
        }
        else if (marks > otherStudent.getMarks()) {
            return 1;
        }
        else {
            return 0;
        }
    }

您可能还想仔细查看该接口的文档。Comparable

于 2013-07-27T16:15:56.923 回答
2

请改用 TreeSet。它有一个带有 Comparator 的构造函数。它将自动对集合进行排序。

转换器:

Set<YourObject> hashSet = getItSomehow();
Set<YourObject> treeSet = new TreeSet<YourObject>(new YourComparator());
treeSet.addAll(hashSet);

文档: http ://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

于 2013-07-27T16:16:36.483 回答
1

对于分数最高的学生:

如果您在 Student 和 Professor 类中实现Comparable接口,您可以简单地使用标准 Java Collections.max

Set<Student> students = ...;
Student highestMarks = Collections.max(students);

如果您在教授的 compareTo 方法中包含学生的最高分,您只需执行

Professor professor = Collections.max(professors)
于 2013-07-27T16:09:18.363 回答
1

用这个:

public Student getStudentWithHigestMarks() {
    Student highest = null;
    for(Professor professor: collectionDept) {
        if(highest == null) {
            highest = professor.Student_Assigned;
        } else if(highest.TotalMarks < professor.Student_Assigned.TotalMarks) {
            highest = professor.Student_Assigned;
        }
    }
    return highest;
}
于 2013-07-27T16:12:33.530 回答