gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:11:59 24 4
gpt4 key购买 nike

我有一个 Person 类,它实现了 Comparable,如下所示,因为我想将 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;
}
}

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

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.教职员对象的优先级应高于学生对象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;
}

最佳答案

让我们首先让您的 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);
}

关于具有 2 个优先级参数的 Java 优先级队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32284470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com