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

1. Member
Join Date
Mar 2010
Posts
19
Rep Power
0

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

Java 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!

2. ## Re: PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();

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.

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?

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.

3. Member
Join Date
Mar 2010
Posts
19
Rep Power
0

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

Originally Posted by Fubarable
That's what this type of queue is for -- any order in, priority order out.
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!

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.

4. Moderator
Join Date
Jul 2010
Location
California
Posts
1,641
Rep Power
10

## 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)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•