Java 返回Hashset中值最高的类对象

Java 返回Hashset中值最高的类对象,java,collections,find,hashset,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;

我有一个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;
    }
}
班级学生{
公共字符串名称;
公共信息;
公共商标;
}
班主任{
公共字符串名称;
公共信息;
指定的公立学生;
}
班级部{
Set collectionDept=new HashSet();
公立学生获得高分(){
返回null;
}
}

我如何使用java找到它?

对于得分最高的学生:

如果您在学生和教授类中实现该接口,您只需使用标准Java即可


对于得分最高的学生:

如果您在学生和教授类中实现该接口,您只需使用标准Java即可

使用以下命令:

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;
}
public Student getstudentwithhighest marks(){
学生最高分数=零;
(教授:收藏部){
如果(最高==null){
最高=指定的教授、学生;
}否则如果(最高.TotalMarks
使用以下方法:

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;
}
public Student getstudentwithhighest marks(){
学生最高分数=零;
(教授:收藏部){
如果(最高==null){
最高=指定的教授、学生;
}否则如果(最高.TotalMarks
实施
可比较的
并对
集合进行排序/操作

例如,在主代码中:

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());
}
。。。这是你的
学生
课程的草稿:

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;
        }
    }
公共班级学生实施可比{
//我们使用封装并将字段的访问权限设置为private
私有字符串名称;
私人互联网;
私人整数标记;
//我们使用封装,并对私有字段具有setter/getter/constructor访问权限
公立学生(字符串名称){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
公共整数getMarks(){
返回标记;
}
公共无效设置标记(整数标记){
这个。标记=标记;
}
//TODO其他设定者/获取者
//这里我们实现了compareTo方法,并根据“marks”字段决定返回哪个int
@凌驾
公共int比较(学生与其他学生){
if(marksotherStudent.getMarks()){
返回1;
}
否则{
返回0;
}
}

您可能还需要仔细查看
Compariable
界面的功能。

实现
Compariable
并对
集合进行排序/操作

例如,在主代码中:

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());
}
…这是你的
学生
课程的草稿:

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;
        }
    }
公共班级学生实施可比{
//我们使用封装并将字段的访问权限设置为private
私有字符串名称;
私人互联网;
私人整数标记;
//我们使用封装,并对私有字段具有setter/getter/constructor访问权限
公立学生(字符串名称){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
公共整数getMarks(){
返回标记;
}
公共无效设置标记(整数标记){
这个。标记=标记;
}
//TODO其他设定者/获取者
//这里我们实现了compareTo方法,并根据“marks”字段决定返回哪个int
@凌驾
公共int比较(学生与其他学生){
if(marksotherStudent.getMarks()){
返回1;
}
否则{
返回0;
}
}

您可能还需要仔细查看
Comparable
接口的。请改用TreeSet。它有一个使用Comparator的构造函数。它将自动对集合进行排序

转换器:

Set<YourObject> hashSet = getItSomehow();
Set<YourObject> treeSet = new TreeSet<YourObject>(new YourComparator());
treeSet.addAll(hashSet);
Set hashSet=getItSomehow();
Set treeSet=new treeSet(new yourcomarator());
树集addAll(hashSet);
文件:

改用树集。它有一个使用比较器的构造函数。它将自动对集合排序

转换器:

Set<YourObject> hashSet = getItSomehow();
Set<YourObject> treeSet = new TreeSet<YourObject>(new YourComparator());
treeSet.addAll(hashSet);
Set hashSet=getItSomehow();
Set treeSet=new treeSet(new yourcomarator());
树集addAll(hashSet);
文件:

谢谢。效果很好。假设两个学生的分数相同,我可以同时显示他们吗?@A.p.S不确定你可以直接通过任何
集合
方法来显示,但是你可以迭代
集合。排序
并选择具有相同初始分数的项目。将为我的an添加一个示例答案必须从
Set
改为
List
。列表有什么帮助,你能举个例子吗?:]事实上,我的评论很愚蠢。你可以迭代
学生
,然后创建一个新的
集合
。对于更复杂的过滤解决方案,你可能需要检查一下。谢谢。它很有效d好的。假设两个学生的分数相同,我可以同时显示这两个分数吗?@A.P.S不确定你可以直接通过
集合
方法来显示,但你可以迭代
集合。排序
并选择初始分数相同的项目。将在我的答案中添加一个示例。必须更改从
Set
List
。列表有什么帮助,你能举个例子吗?:]事实上,我的评论很傻。你可以重复一下