File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# Regarding PriorityQueue

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
im having soubt in this program..

This code produces something like this:
1 3 5 6 7 8 9
size 7
peek 9
size 7
poll 9
size 6
8 7 6 5 3 1 null

1) how does inverse sort work.what is taken in as integer one and integer two??

2)here the initial capacity is 10?wat is the significance of it?does it mean that the priority queue will contain 10 elements?

3)i understand that it is reverse sorted order.but i dont understand how it happens??

i dont understand the comparator part of it.....

a) wat does this code do:
PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10,pqs); // use Comparator
for(int x : ia) // load queue
pq2.offer(x)

b)class PQ {
static class PQsort
implements Comparator<Integer> { // inverse sort
public int compare(Integer one, Integer two) {
return two - one;

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

kiruthigha rajan wrote:
1) how does inverse sort work.what is taken in as integer one and integer two??

If comparator <= 0 than PQ puts one before two else two before one. in your case PQ is maximum priority queue. i.e, maintain objects descending order.

kiruthigha rajan wrote:
2)here the initial capacity is 10?wat is the significance of it?does it mean that the priority queue will contain 10 elements?

capacity is different from size. you can think, capacity of 10 allocates 10 buckets for 10 elements to store.
kiruthigha rajan wrote:
3)i understand that it is reverse sorted order.but i dont understand how it happens??

Actually it is not reversing an order. instead defines an order.( maximum heap)

I recommend you to go through java.util.PriorityQueue JavaDoc
<edit>corrected typo</edit>

I agree. Here's the link: http://aspose.com/file-tools

subject: Regarding PriorityQueue