Search code examples
javacomparisonpriority-queue

Should I use Comparator or Comparable when trying to use a Priority Queue with this generic class?


When trying to give generic objects priority in a Priority Queue what can I use to compare them? Can I define and use an overridden CompareTo method from the Comparable interface or an overridden Compare method from the Comparator interface? Or could I use one or the other? Thanks

Here are the instance variables, constructor of the class, and the current compareTo method.

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;
    }  }

Solution

  • As you already have implemented compareTo, you have Comparable Flight or Event instances.

    That means you're set to use it with Comparable objects. All of the below should work:

    Queue<Event> eventQueue = new PriorityQueue<>();
    eventQueue.add(new Flight(scheduledTime, eventType, identifier));
    

    Or:

    List<Flight> flightList = Arrays.asList(new Flight(scheduledTime, 
                               eventType, identifier));
    Queue<Flight> flightQueue = new PriorityQueue<>(flightList);
    

    Or:

    List<Event> eventList = ...;
    Queue<Event> eventQueue = new PriorityQueue<>(eventList);
    

    The PiorityQueue class should be able to handle the priority according to the order dictated by your compareTo ordering.

    Note: if your List<Event> has objects of other classes that implement Event, then you must make sure that those other classes also have compareTo(Event otherFlight). Otherwise, the priority queue may raise exceptions at runtime.
    The best option may be to just declare Flight as implementing Comparable<Flight> and instantiate a PriorityQueue<Flight> queue.