Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Regarding PriorityQueue

kiruthigha rajan
Ranch Hand
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
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>