This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding PriorityQueue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding PriorityQueue" Watch "Regarding PriorityQueue" New topic
Author

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;


please explain in detail thanks in advance
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>
 
 
subject: Regarding PriorityQueue