Java 在尝试使用此泛型类的优先级队列时,是否应使用Comparator或Comparable?
当尝试在优先级队列中为泛型对象赋予优先级时,我可以使用什么来比较它们?我可以从Comparable接口定义并使用重写的CompareTo方法,还是从Comparator接口定义并使用重写的CompareTo方法?或者我可以用一个还是另一个?谢谢 下面是实例变量、类的构造函数和当前的compareTo方法Java 在尝试使用此泛型类的优先级队列时,是否应使用Comparator或Comparable?,java,comparison,priority-queue,Java,Comparison,Priority Queue,当尝试在优先级队列中为泛型对象赋予优先级时,我可以使用什么来比较它们?我可以从Comparable接口定义并使用重写的CompareTo方法,还是从Comparator接口定义并使用重写的CompareTo方法?或者我可以用一个还是另一个?谢谢 下面是实例变量、类的构造函数和当前的compareTo方法 private LocalTime scheduledTime; //the scheduled time of the flight private Event.EventType event
private LocalTime scheduledTime; //the scheduled time of the flight
private Event.EventType eventType; //the event type of the flight (arrival or departure)
private String identifier; // the identifier of the flight
private LocalTime actualTime; //the actual time the flight uses a runway
private Runway runwayUsed; //the runway the flight used to arrive or depart
private int reserveTime; // time the flight uses to reserve a runway
private LocalTime runwayAvailableTime;
/**
* Constructor
* @param scheduledTime the scheduled time of the flight
* @param eventType the event of the flight (arrival or departure)
* @param identifier the identifier of the flight
*/
protected Flight(String scheduledTime, String eventType, String identifier) {
this.scheduledTime = LocalTime.parse(scheduledTime);
this.eventType = EventType.valueOf(eventType);
this.identifier = identifier;
this.actualTime = null;
this.runwayUsed = null;
}
//Here is the compareTo method I am currently using. Should I use compare //from the Comparator interface instead?
@Override
public int compareTo(Event otherFlight) {
Event tmpFlight = (Event) otherFlight;
if(this.scheduledTime.compareTo(tempFlight.getScheduledTime()) == 0) {
if(this.eventType.compareTo(tempFlight.getEvent()) == 0){
return 0;
}
else if(this.eventType.compareTo(tempFlight.getEvent()) > 0){
return 1;
}
else {
return -1;
}
}
else if(this.scheduledTime.compareTo(tempFlight.getScheduledTime()) < 0) {
return -1;
}
else {
return 1;
} }
private LocalTime scheduledTime//航班的预定时间
private Event.EventType EventType//航班的事件类型(到达或离开)
私有字符串标识符;//航班的标识符
私有本地时间实际时间//航班使用跑道的实际时间
使用私人跑道//飞机用来到达或离开的跑道
私有int reserveTime;//航班预定跑道的时间
专用本地时间跑道可用时间;
/**
*建造师
*@param scheduledTime航班的预定时间
*@param eventType航班事件(到达或离开)
*@param identifier航班的标识符
*/
受保护航班(字符串scheduledTime、字符串eventType、字符串标识符){
this.scheduledTime=LocalTime.parse(scheduledTime);
this.eventType=eventType.valueOf(eventType);
this.identifier=标识符;
this.actualTime=null;
this.runwayUsed=null;
}
//下面是我目前使用的compareTo方法。我应该使用Comparator接口中的compare//吗?
@凌驾
公共整数比较(事件其他航班){
事件tmpFlight=(事件)其他航班;
if(this.scheduledTime.compareTo(tempfright.getScheduledTime())==0){
if(this.eventType.compareTo(tempfright.getEvent())==0){
返回0;
}
else if(this.eventType.compareTo(tempfright.getEvent())>0){
返回1;
}
否则{
返回-1;
}
}
else if(this.scheduledTime.compareTo(tempfright.getScheduledTime())<0){
返回-1;
}
否则{
返回1;
} }
由于您已经实现了compareTo
,因此您有了可比较的航班
或事件
实例
这意味着您已将其设置为与可比较的
对象一起使用。以下所有操作都应有效:
Queue<Event> eventQueue = new PriorityQueue<>();
eventQueue.add(new Flight(scheduledTime, eventType, identifier));
Queue eventQueue=new PriorityQueue();
添加(新航班(scheduledTime、eventType、标识符));
或:
List flightList=Arrays.asList(新航班(scheduledTime,
事件类型,标识符);
队列flightQueue=新的优先级队列(flightList);
或:
List eventList=。。。;
队列事件队列=新的优先级队列(事件列表);
PiorityQueue
类应该能够根据您的compareTo
排序所指定的顺序来处理优先级
注意:如果您的列表中有实现事件的其他类的对象,那么您必须确保这些其他类也有比较(事件其他飞行)
。否则,优先级队列可能在运行时引发异常。
最好的选择可能是将航班
声明为实现可比
,并实例化一个优先队列
队列。您可以使用其中一个。这是你的选择,你实施什么…在做决定之前先读一下,它们是不同的。在上面的例子中,我应该使用哪一种?你的航班
等级是如何声明的?为什么要用compareTo(Event)
而不是compareTo(Flight)
?根据Prabin Paudel发布的信息,我认为比较飞行对象的方式不是“自然顺序”,所以我应该使用Comparator界面的比较方法,对吗?谢谢您的回复。因为我不是按“自然顺序”进行比较,所以我认为我必须实现比较器接口并使用比较方法。感谢您让我知道,在使用航班或事件对象的优先级队列时,我可以使用Comparable接口的compareTo方法。
List<Flight> flightList = Arrays.asList(new Flight(scheduledTime,
eventType, identifier));
Queue<Flight> flightQueue = new PriorityQueue<>(flightList);
List<Event> eventList = ...;
Queue<Event> eventQueue = new PriorityQueue<>(eventList);