简体   繁体   中英

How can I use Collections.sort() if I implement Comparable interface in the subclass?

I came up with a small problem while doing my homework.

I have a Person class

public class Person {
    protected String code;
    protected String name;
}

And a subclass called Student

public class Student extends Person implements Comparable<Student>{

    private double grade;

    @Override
    public int compareTo(Student that){
        return Double.compare(grade, that.grade);
    }
}

The teacher wanted us to create a Professor class to manage Student using ArrayList

public class Professor {
    private List<Person> arr;

    public Professor() {
       arr = new ArrayList();
    }
}

After adding about ten students to the ArrayList and we're asked to sort that list. The problem is, as far as I know, I cannot use Collections.sort(arr) since I declared it List< Person >, not List< Student >.

Did I misunderstand anything? Is there any way to use Collections.sort() in this situation or I have to write a method to sort it manually?

And he asked us to implement the Comparable interface in the class Student, also asked us to create List< Person > instead of List< Student >

EDIT: THANK YOU VERY MUCH. APPRECIATE ALL OF THE ANSWERS AND COMMENTS. THE ABOVE PROBLEM HAS BEEN SOLVED BY DOWNCAST List< Person > to List < Student >

The teacher wanted us to create a Professor class to manage Student using ArrayList

So replace

private List<Person> arr;

by :

private List<Student> students;

In this way, you could invoke Collections.sort(students) as student refers to a List of Comparable elements ( Student implementing the Comparable interface).


Edit after the comment :

The requirement is not very logical : you have to sort something ( List<Person> ) that is not sortable (as only Student implements Comparable ).

In theses conditions, you have three ways :

  • the simplest and the cleanest way (if you may use it).
    Make Person an abstract class that implements Comparable but keep it abstract in.
    In this way you would not need to use any cast. You could directly invoke Collections.sort() with a List<Person>

  • force the downcast from List<Person> to List<Student> and invoke then Collections.sort() .

  • perform yourself the sort but you should still do some casts if you want to use the compareTo() method of the Student class as Person is not a Comparable instance.

It is not allowing as person because the collection sort required to implement implements Comparable<Person> as the API is.

public static <T extends Comparable<? super T>> void sort(List<T> list) {
        list.sort(null);
    }

One way is as mentioned below post I am providing you another approach for sorting list using Comparator

class SortbyGrade implements Comparator<Student>
{
    // Used for sorting in ascending order of
    // roll number
    public int compare(Student a, Student b)
    {
         return Double.compare(a.grade, b.grade);
    }
}

//collection sort

Collections.sort(arr, new SortbyGrade());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM