# PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();

• 09-19-2011, 03:26 AM
java_on_fire
PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();
Code:

```package priorityqueue; import java.util.*; public class PQ {                 static class PQsort implements Comparator<Integer>{                 public int compare(Integer one, Integer two) {                         return two-one;                 }         }         /**         * @param args         */         public static void main(String[] args) {                 int[] ia = {1, 5, 3, 7, 6, 9, 8 };                                 PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();                                 for( int x : ia)                         pq1.offer(x);                                                 System.out.println(pq1);                                 for ( int x : ia)                         System.out.print(pq1.poll());                                 System.out.print("\n" + pq1);                                 PQsort pqs = new PQsort();                                 PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10, pqs);                                 System.out.print("\n" + pq2);                 for(int x : ia)                         pq2.offer(x);                 System.out.print("\n" + pq2 + "\n");                                 for (int x : ia)                         System.out.print(pq2.poll() + " ");         } }```
1. from running the code above, i understood that the sorting would be done when a method poll() is called but why is it sorting when poll() is called? isn't it too late to sort after it removed all elements from priorityqueue? it doesn't make sense!

2. also how is the sorting done? somehow a call is being made from inside the poll() to siftDown then another call to siftDownUsingComparator. Please look at method above and advise how is it sorting!!

3. the initial capacity in PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10, pqs); is set to 10 but it still works if i change it to 1, but if i change it to 0 i will get an exception! i guess that's just the initial capacity and i can put one in there, correct!
• 09-19-2011, 03:39 AM
Fubarable
Re: PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();
Quote:

Originally Posted by java_on_fire
1. from running the code above, i understood that the sorting would be done when a method poll() is called but why is it sorting when poll() is called? isn't it too late to sort after it removed all elements from priorityqueue? it doesn't make sense!

That's what this type of queue is for -- any order in, priority order out.

Quote:

2. also how is the sorting done? somehow a call is being made from inside the poll() to siftDown then another call to siftDownUsingComparator. Please look at method above and advise how is it sorting!!
I'm not sure how it's done. To know for sure, we'd have to look at the source code -- which is available to all. Why not take a look at it and report back to us?

Quote:

3. the initial capacity in PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10, pqs); is set to 10 but it still works if i change it to 1, but if i change it to 0 i will get an exception! i guess that's just the initial capacity and i can put one in there, correct!
The API will tell you what the initial capacity parameter represents and what to expect if you try to construct a queue with an initial capacity of < 1.
• 09-19-2011, 03:46 AM
java_on_fire
Re: PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();
Quote:

Originally Posted by Fubarable
That's what this type of queue is for -- any order in, priority order out.

Quote:

I'm not sure how it's done. To know for sure, we'd have to look at the source code -- which is available to all. Why not take a look at it and report back to us?
i did look at source code, but i couldn't understand it! very complicated code for a beginner!

Quote:

The API will tell you what the initial capacity parameter represents and what to expect if you try to construct a queue with an initial capacity of < 1.
ok, i looked at source code, if you pass 0, the method will through an exception, i just wanted someone to confirm that i understood that the initial capacity is able to change to accommodate more.
• 09-19-2011, 03:46 AM
doWhile
Re: PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();
See the API page for this class, it has a lot of information which should answer quite a few of your questions:
PriorityQueue (Java Platform SE 6)