Sorting PriorityQueue

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Sorting PriorityQueue



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 comes from). A priorityQueue can be made with a comparator in the constructor and I have tried this but it does not work.



Queue class:


public JavaPriorityFlightQueue()
super();
flights = new PriorityQueue(5, new SortQueueViaPriority());



Comparator:


import java.util.Comparator;

public class SortQueueViaPriority implements Comparator

public int compare(Object o1, Object o2)
Flight f1 = (Flight) o1;
Flight f2 = (Flight) o2;

if( f1 == null



Priority is an int value which is part of the flight class. I test this.


JavaPriorityFlightQueue flightQueue = new JavaPriorityFlightQueue();

Flight flight1 = new Flight("0001",9);
Flight flight2 = new Flight("0002",7);
Flight flight3 = new Flight("0003",1);
Flight flight4 = new Flight("0004",2);
Flight flight5 = new Flight("0005",1);



However the PriorityQueue is not sorted, and when I check it the value 9 is never compared to anything and the result is nothing is sorted. the compare class SortQueueViaPriority is copy and pasted from another class where the class sorts perfectly.





See this answer: stackoverflow.com/questions/683041/…
– Miljen Mikic
Nov 12 '12 at 15:46




3 Answers
3



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


Flightname='0001', priority=9
Flightname='0002', priority=7
Flightname='0004', priority=2
Flightname='0003', priority=1
Flightname='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.





@dacwe Correct, that is how the OP implemented it in any case.
– Peter Lawrey
Nov 12 '12 at 15:41





Odd for me it prints
– user1817988
Nov 12 '12 at 15:43





Flight [flightID=0003, priority=1] Flight [flightID=0005, priority=1] Flight [flightID=0002, priority=7] Flight [flightID=0001, priority=9] Flight [flightID=0004, priority=2]
– user1817988
Nov 12 '12 at 15:43





As I note at the end, that is to be expected.
– Peter Lawrey
Nov 12 '12 at 15:45





oh ok thank you.
– user1817988
Nov 12 '12 at 15:46



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


Iterator


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.


toString



And in the Java doc of PriorityQueue


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.



Instead of Comparator just use Comparable interface.


Comparator


Comparable



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.


compareTo()



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;






That is not the problem.
– Don Roby
Nov 12 '12 at 16:04





oh is it..ok this way i worked ok.
– Chaitu
Nov 14 '12 at 9:10






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard