2

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;
    }  }
Boognish
  • 103
  • 1
  • 2
  • 13
  • You can use either. It's your choice what you implement... – ernest_k Apr 01 '18 at 15:01
  • Read this https://stackoverflow.com/questions/420223/what-is-the-difference-between-compare-and-compareto before making a decision, they are not same. – royalghost Apr 01 '18 at 15:13
  • Which should I use in the case above? – Boognish Apr 01 '18 at 15:22
  • How is your `Flight` class declared? Why do you have `compareTo(Event)` instead of `compareTo(Flight)`? – ernest_k Apr 01 '18 at 15:27
  • Based on the information that Prabin Paudel posted, I believe the way in which I would like to compare the Flight objects is not in 'natural order', so I should use the compare method of the Comparator interface, correct? – Boognish Apr 01 '18 at 15:29
  • @ Ernest Kiwele it is defined as: public class Flight implements Event, Comparable, Cloneable{ – Boognish Apr 01 '18 at 15:30
  • As of Java 8, you don’t need to write a method. You can just use `new PriorityQueue(Comparator.comparing(Event::getScheduledTime).thenComparing(Event::getEventType))`. – VGR Apr 01 '18 at 17:30

1 Answers1

1

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.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • Thank you for the response. Because I was not comparing by 'natural order' I thought that I had to implement the Comparator interface and use the compare method. Thank you for letting me know that I can use the compareTo method of the Comparable interface when using a Priority Queue of Flight or Event objects. – Boognish Apr 01 '18 at 15:46