具有2个优先级参数的Java优先级队列

时间:2015-08-29 09:12:37

标签: java comparable

我有一个实现Comparable的Person类,如下所示,因为我想将Person的对象放在优先级队列中。

public class Student implements Comparable{
    private String fullName;
    private Date registrationDate;

public Person(String fullName){
    this.fullName = fullName;
}

public String getFullName() {
    return fullName;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}

public Date getRegistrationDate() {
        return registrationDate;
    }
    public void setRegistrationDate(Date registrationDate) {
        this.registrationDate = registrationDate;
    }

@Override
    public int compareTo(Object obj) {
        Person person = (Person) obj;

        if(person instanceof Staff){
            return 1; 
        }else if(person instanceof Student){
            return -1;
        }
        else if(getRegistrationDate().before(person.getRegistrationDate())){
            return 1;
        }else if(getRegistrationDate().after(person.getRegistrationDate())){
            return -1;
        }
        return 0;
    }
}

我有两个扩展Person的类,如下所示

public class Staff extends Person{
    public Staff(String fullName){
        this.fullName = fullName;
     }
}

public class Student extends Member{
    public Student(String fullName){
        this.fullName = fullName;
     }
}

在main方法中,我正在创建Staff的对象和学生的对象,设置对象的注册日期并将它们放入优先级队列

public class School {

    public static void main(String args[]) {

        //list of students
        Student student1 = new Student("John Kent");
        Date dateStudent1Joined = new GregorianCalendar(2014, Calendar.JULY, 1).getTime();
        student1.setRegistrationDate(dateStudent1Joined);

        Student student2 = new Student("Peter Tush");
        Date dateStudent2Joined = new GregorianCalendar(2014, Calendar.JULY, 2).getTime();
        student2.setRegistrationDate(dateStudent2Joined);

        Student student3 = new Student("Mike Monroe");
        Date dateStudent3Joined = new GregorianCalendar(2014, Calendar.JULY, 3).getTime();
        student3.setRegistrationDate(dateStudent3Joined);

        Student student4 = new Student("Tom Johnson");
        Date dateStudent4Joined = new GregorianCalendar(2014, Calendar.JULY, 4).getTime();
        student4.setRegistrationDate(dateStudent4Joined);

        Student student5 = new Student("Tony Spencer");
        Date dateStudent5Joined = new GregorianCalendar(2014, Calendar.JULY, 5).getTime();
        student5.setRegistrationDate(dateStudent5Joined);

        //list of staff
        Staff staff1 = new Staff("Luke Clint");
        Date dateStaff1Joined = new GregorianCalendar(2014, Calendar.JULY, 6).getTime();
        staff1.setRegistrationDate(dateStaff1Joined);

        Staff staff2 = new Staff("Ron West");
        Date dateStaff2Joined = new GregorianCalendar(2014, Calendar.JULY, 7).getTime();
        staff2.setRegistrationDate(dateStaff2Joined);

        Staff staff3 = new Staff("Jim Gary");
        Date dateStaff3Joined = new GregorianCalendar(2014, Calendar.JULY, 8).getTime();
        staff3.setRegistrationDate(dateStaff3Joined);


        //create a queue data structure to hold Persons in school
        PriorityQueue<Person> schoolQueue = new PriorityQueue<Person>();
        //add students to queue
        schoolQueue.offer(student1);
        schoolQueue.offer(student2);
        schoolQueue.offer(student3);
        schoolQueue.offer(student4);
        schoolQueue.offer(student5);
        //add staff to queue
        schoolQueue.offer(staff1);
        schoolQueue.offer(staff2);
        schoolQueue.offer(staff3);

        //print names of people in queue
        for(Member member : clubQueue){
            String memberName = member.getFullName();
            System.out.println(memberName);
        }

    }
}

我的优先级队列应遵循3条规则 1.Staff对象应该具有比学生对象更高的优先级 2.具有较早注册日期的员工应优先于具有较晚注册日期的员工 3.具有较早注册日期的学生应优先于具有较晚注册日期的学生。

我得到的输出目前没有产生预期的结果。人员对象的优先级高于学生对象,但根据日期的优先级不起作用。我理解compareTo方法中的规则是问题所在,我该如何改进呢? 这是为了方便而再次

@Override
        public int compareTo(Object obj) {
            Person person = (Person) obj;

            if(person instanceof Staff){
                return 1; 
            }else if(person instanceof Student){
                return -1;
            }
            else if(getRegistrationDate().before(person.getRegistrationDate())){
                return 1;
            }else if(getRegistrationDate().after(person.getRegistrationDate())){
                return -1;
            }
            return 0;
        }

3 个答案:

答案 0 :(得分:3)

让我们首先让您的Person类使用泛型类型而不是原始类型:

public class Person extends Comparable<Person>

然后让我们为课程分配优先级。你没有告诉那些既不是工作人员也不是学生的“裸”人应该与其他人比较。因此,我认为不应该存在“裸”的人,因此你的Person类应该是抽象的:

public abstract class Person extends Comparable<Person>

然后你希望每个班级都有优先权。所以让我们实现它,而不是依赖于丑陋的instanceof

protected abstract int getPriority();

工作人员应该来学生,所以在工作人员:

@Override
protected int getPriority() {
    return 0;
}

和学生:

@Override
protected int getPriority() {
    return 1000;
}

现在让我们实现compareTo方法:

public int compareTo(Person other) {
    int result = Integer.compare(this.getPriority(), other.getPriority());
    if (result == 0) {
        result = this.getRegistrationDate().compareTo(other.getRegistrationDate())
    }
    return result;
}

请注意,添加另一种Person是微不足道的。您只需要在getPriority中返回适当的值,就不必更改比较代码。

另请注意,compareTo现在将Person作为参数,并且由于正确的泛型类型,编译器现在会阻止您执行person.compareTo("foo")等愚蠢的操作。

如果使用Java 8,您的compareTo方法可能更简单:

private static final Comparator<Person> COMPARATOR = 
    Comparator.comparingInt(Person::getPriority) 
              .thenComparing(Person::getRegistrationDate);

@Override
public int compareTo(Person other) {
    return COMPARATOR.compare(this, other);
}

答案 1 :(得分:1)

注意: Comparable是一个通用接口。将泛型用于您的使用。

让我们来看看你的compareTo methodcompareTo方法首先检查天气objinstanceof Staff还是Student

schoolQueue包含StaffStudent个对象。之一:

if(person instanceof Staff){
  return 1; 

或:

}else if(person instanceof Student){
  return -1;
}

已执行。所以最后两个else if块永远不会被执行。

<强>解决方案:

// If the object on which compareTo() is applied is of type Student and person
// is of type Staff, return -2
// -2 indicates that Staff has priority over student
if(this instanceof Student && person instanceof Staff){
  return -2; 
}
// If the object on which compareTo() is applied is of type Staff and person
// is of type Student, return 2
else if(this instanceof Staff && person instanceof Student){
  return 2;
}
// If both are of same type, prioritize the one who registered early
else {
  return (this.getRegistrationDate()).compareTo(person.getRegistrationDate());
}

答案 2 :(得分:0)

一个问题是你总是假设一个人是一名工作人员,一个是学生。可能两者都属于同一类。

 if(this instance Student && person instanceof Staff) {
        return 1; // Student has priority
 } else if (this instance Staff && person instanceof Student){
        return -1; // Student has priority
 }
 return getRegistrationDate().compareTo(person.getRegistrationDate());