返回Hashset中具有最高值的类对象

时间:2013-07-27 16:05:08

标签: java collections find hashset

我有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找到它?

3 个答案:

答案 0 :(得分: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界面的documentation

答案 1 :(得分:1)

对于得分最高的学生:

如果您在Student和Professor课程中实现Comparable界面,则只需使用标准Java Collections.max

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

如果你在Professor's compareTo方法中包含学生的最高分,那么你 只需做

Professor professor = Collections.max(professors)

答案 2 :(得分: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;
}