Sort PriorityQueue

I have a problem with PriorityQueues, as I am convinced that he orders a priority, but I am not sure what the priority is (I mean what the value is and where it comes from). PriorityQueue can be done using a comparator in the constructor, and I 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 || f2 == null ){ if( f1 == f2 ) return 0; else if( f2 == null) return +1; else return -1; } Integer i1 = (Integer) f1.getPriority(); Integer i2 = (Integer) f2.getPriority(); return i2.compareTo(i1); } } 

Priority is an int value that is part of the flight class. I check it out.

 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 not sorted. the comparison class SortQueueViaPriority is copied and pasted from another class where the class is perfectly sorted.

+4
source share
3 answers

I suggest you try the following example. If you use PriorityQueue as a queue, the entries are deleted 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 the entries so that only the first item is the smallest. If you go one by one, you will see all the elements, but they may or may not be in order.

+4
source

Iterator problem. As documented in Java doc of PriorityQueue#iterator

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

Since toString uses an iterator, it will not print in order. Or, if you use an iterator-based loop, then it will be fine.

And in the Java document PriorityQueue

Polling, retrieving, viewing, and a queue access item to access the item at the head of the queue.

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

+2
source

Instead of Comparator just use the Comparable interface.

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

Similar:

 @Override public int compareTo(Object obj) { // TODO Auto-generated method stub Flight f = (Flight)obj; if(this.a <fa){ return 1; }else{ return -1; } } 
0
source

Source: https://habr.com/ru/post/1445549/


All Articles