尝试将优先级队列与此通用 class 一起使用时,我应该使用 Comparator 还是 Comparable?
Should I use Comparator or Comparable when trying to use a Priority Queue with this generic class?
当尝试在优先队列中赋予通用对象优先级时,我可以使用什么来比较它们?我能否定义和使用来自 Comparable 接口的重写 CompareTo 方法或来自 Comparator 接口的重写 Compare 方法?或者我可以使用其中之一吗?谢谢
下面是class的实例变量、构造函数和当前的compareTo方法。
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;
} }
由于您已经实施了 compareTo
,因此您有 Comparable
Flight
或 Event
个实例。
这意味着您可以将它与 Comparable
对象一起使用。以下所有内容都应该有效:
Queue<Event> eventQueue = new PriorityQueue<>();
eventQueue.add(new Flight(scheduledTime, eventType, identifier));
或者:
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);
PiorityQueue
class 应该能够根据您的 compareTo
订单规定的顺序处理优先级。
注意:如果你的List<Event>
有其他classes的对象实现了Event
,那么你必须确保那些其他的classes classes 还有 compareTo(Event otherFlight)
。否则,优先级队列可能会在运行时引发异常。
最好的选择可能是将 Flight
声明为实现 Comparable<Flight>
并实例化一个 PriorityQueue<Flight>
队列。
当尝试在优先队列中赋予通用对象优先级时,我可以使用什么来比较它们?我能否定义和使用来自 Comparable 接口的重写 CompareTo 方法或来自 Comparator 接口的重写 Compare 方法?或者我可以使用其中之一吗?谢谢
下面是class的实例变量、构造函数和当前的compareTo方法。
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;
} }
由于您已经实施了 compareTo
,因此您有 Comparable
Flight
或 Event
个实例。
这意味着您可以将它与 Comparable
对象一起使用。以下所有内容都应该有效:
Queue<Event> eventQueue = new PriorityQueue<>();
eventQueue.add(new Flight(scheduledTime, eventType, identifier));
或者:
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);
PiorityQueue
class 应该能够根据您的 compareTo
订单规定的顺序处理优先级。
注意:如果你的List<Event>
有其他classes的对象实现了Event
,那么你必须确保那些其他的classes classes 还有 compareTo(Event otherFlight)
。否则,优先级队列可能会在运行时引发异常。
最好的选择可能是将 Flight
声明为实现 Comparable<Flight>
并实例化一个 PriorityQueue<Flight>
队列。