Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a PriorityQueue?

How do I get a PriorityQueue to sort on what I want it to sort on?

Also, is there a difference between the offer and add methods?

like image 603
Svish Avatar asked Mar 25 '09 19:03

Svish


People also ask

How do you implement PriorityQueue?

Priority Queues can be implemented using common data structures like arrays, linked-lists, heaps and binary trees. The list is so created so that the highest priority element is always at the head of the list. The list is arranged in descending order of elements based on their priority.

How does Java PriorityQueue work?

A priority queue in Java is a special type of queue wherein all the elements are ordered as per their natural ordering or based on a custom Comparator supplied at the time of creation.

How do you write a PriorityQueue in Java?

PriorityQueue<E> pq = new PriorityQueue<E>(Collection<E> c); 3. PriorityQueue(int initialCapacity): Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering. PriorityQueue<E> pq = new PriorityQueue<E>(int initialCapacity);

How do I sort PriorityQueue?

There is an obvious way to do sorting with priority queues: Take the items that you want to sort, and insert them into the priority queue (using the item itself as its own priority). Then remove items from the priority queue until it is empty. The items will come off the queue in order from largest to smallest.


1 Answers

Use the constructor overload which takes a Comparator<? super E> comparator and pass in a comparator which compares in the appropriate way for your sort order. If you give an example of how you want to sort, we can provide some sample code to implement the comparator if you're not sure. (It's pretty straightforward though.)

As has been said elsewhere: offer and add are just different interface method implementations. In the JDK source I've got, add calls offer. Although add and offer have potentially different behaviour in general due to the ability for offer to indicate that the value can't be added due to size limitations, this difference is irrelevant in PriorityQueue which is unbounded.

Here's an example of a priority queue sorting by string length:

// Test.java import java.util.Comparator; import java.util.PriorityQueue;  public class Test {     public static void main(String[] args) {         Comparator<String> comparator = new StringLengthComparator();         PriorityQueue<String> queue = new PriorityQueue<String>(10, comparator);         queue.add("short");         queue.add("very long indeed");         queue.add("medium");         while (queue.size() != 0) {             System.out.println(queue.remove());         }     } }  // StringLengthComparator.java import java.util.Comparator;  public class StringLengthComparator implements Comparator<String> {     @Override     public int compare(String x, String y) {         // Assume neither string is null. Real code should         // probably be more robust         // You could also just return x.length() - y.length(),         // which would be more efficient.         if (x.length() < y.length()) {             return -1;         }         if (x.length() > y.length()) {             return 1;         }         return 0;     } } 

Here is the output:

short

medium

very long indeed

like image 155
Jon Skeet Avatar answered Oct 21 '22 03:10

Jon Skeet