Sorting PriorityQueue

前端 未结 3 756
难免孤独
难免孤独 2020-12-18 08:50

I am having a problem with PriorityQueues, as I am lead to believe it orders on priority however I am not sure what the priority is (I mean what the value is and where it co

相关标签:
3条回答
  • 2020-12-18 09:24

    Issue is Iterator.As Documented in Java doc of PriorityQueue#iterator

    Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

    As toString uses iterator it will not get printed in order. Or if you use loop based on iterator then also it will be in order.

    And in the Java doc of PriorityQueue

    The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.

    To get results in order you will have to use one of these methods.

    0 讨论(0)
  • 2020-12-18 09:35

    Instead of Comparator just use Comparable interface.

    Your Flight class should implement Comparable interface. Then you need to override the compareTo() method. In that method you can add your own logic for sorting based on the property you need.

    Just like this way:

    @Override
    public int compareTo(Object obj) {
        // TODO Auto-generated method stub
        Flight f = (Flight)obj;
        if(this.a <f.a){
        return 1;
        }else{
            return -1;
        }
    }
    
    0 讨论(0)
  • 2020-12-18 09:50

    I suggest you try the following example. If you use PriorityQueue as a queue, the entries are removed in order.

    import java.util.Comparator;
    import java.util.PriorityQueue;
    
    public class Main {
        public static void main(String... args) {
            PriorityQueue<Flight> flights = new PriorityQueue<Flight>(5, new SortQueueViaPriority());
            flights.add(new Flight("0001", 9));
            flights.add(new Flight("0002", 7));
            flights.add(new Flight("0003", 1));
            flights.add(new Flight("0004", 2));
            flights.add(new Flight("0005", 1));
    
            while (!flights.isEmpty())
                System.out.println(flights.remove());
        }
    }
    
    class SortQueueViaPriority implements Comparator<Flight> {
        @Override
        public int compare(Flight f1, Flight f2) {
            return Integer.compare(f2.getPriority(), f1.getPriority());
        }
    }
    
    class Flight {
        private final String name;
        private final int priority;
    
        Flight(String name, int priority) {
            this.name = name;
            this.priority = priority;
        }
    
        public int getPriority() {
            return priority;
        }
    
        @Override
        public String toString() {
            return "Flight{" +
                    "name='" + name + '\'' +
                    ", priority=" + priority +
                    '}';
        }
    }
    

    prints

    Flight{name='0001', priority=9}
    Flight{name='0002', priority=7}
    Flight{name='0004', priority=2}
    Flight{name='0003', priority=1}
    Flight{name='0005', priority=1}
    

    Note: PriorityQueue sorts entries such that only the first element will be the smallest. If you iterate over the queue, you will see all the elements, but they may or may not be in order.

    0 讨论(0)
提交回复
热议问题