Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding PriorityQueue

 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;


please explain in detail thanks in advance
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic