1

I have HashSet which contain list of objects as Student and professor and department. It needs to check which student have the highest marks and professor which have student with highest marks in total. The sample code represent like

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

How can I find this using java?

Rui Vieira
  • 5,253
  • 5
  • 42
  • 55
A.P.S
  • 1,124
  • 4
  • 17
  • 36

4 Answers4

2

Implement Comparable and sort/manipulate your Collection.

For instance in your main code:

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

Output:

good
good
otherGood

... and here's a draft of your Student class:

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

You may also want to take a closer look at the documentation for the Comparable interface.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • Thanks. It just worked fine. Suppose if two student has same marks, can I display both of them anyhow? – A.P.S Jul 27 '13 at 17:59
  • @A.P.S not sure you can do that through any of the `Collections` methods directly, but you could iterate over `Collections.sort` and pick the items as long as they have the same initial score. Will add an example to my answer. Will have to change from `Set` to `List`. – Mena Jul 27 '13 at 18:03
  • How does list help, can you give some example. :] – A.P.S Jul 27 '13 at 18:13
  • Actually, my comment is dumb. You can just iterate over `students` and create a new `Set` with the ones having the best mark. For more complex filtering solutions, you might want to check [here](http://stackoverflow.com/questions/122105/java-what-is-the-best-way-to-filter-a-collection) – Mena Jul 27 '13 at 18:13
2

Use a TreeSet instead. It has a constructor taking a Comparator. It will automatically sort the Set.

Converter:

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

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

Thangnv
  • 805
  • 3
  • 11
  • 22
1

For student with the highest marks:

If you implement the Comparable interface in the Student and Professor classes you can simply use the standard Java Collections.max

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

If you include the student's highest mark in the Professor's compareTo method, you simply do

Professor professor = Collections.max(professors)
Rui Vieira
  • 5,253
  • 5
  • 42
  • 55
1

Use this:

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;
}
pocoLoco
  • 64
  • 3