This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

priorityqueue class

 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

output

1 3 5 6 7 8 9
size 7
peek 9
size 7
poll 9
size 6
8 7 6 5 3 1 null

poll method, which returns the highest priority entry in pq1 AND removes the entry from the queue. (does it removed the entry? I couldn't understand)

please explain the above code.

Thank you
saritha
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

The poll method removes the head of the queue and it is sorted in natual order until you pass the object which implements comparator interface. Look the output is

1 3 5 6 7 8 9

after executing System.out.print(pq1.poll() + " ");

But after that you are again creating a new Priority Queue but this time you pass two things to the constructor, the first one is the capacity and second one is the object of class which implements comparator and defines how the elements should be sorted.
Now you populate the queue again and then call the size method which returns 7, since you populated 7 elements using
pq2.offer(x);
If you call peek then it won't remove the front element but if you call poll then it is going to remove the front element and return it. That is why after the second call to size followed by peek returns you same size(7) while the next call to size followed by poll method call returns you 6.

Hope I am clear.
Regards
Padma
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have posted same example post(dolly shah). If you want to be more clear, go & see my post. There are good replies on that, so that you can get more idea. I don't know how to give a link.
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I guess you are referring to this.

web page

Regards
Padma
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when the comparator method compare() is invoked in the above code?
i.e(class PQ)
I mean, How does it invoke the compare() method with the following statement
PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10,pqs);

Thanks
samura
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

when the comparator method compare() is invoked in the above code?
i.e(class PQ)
I mean, How does it invoke the compare() method with the following statement
PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10,pqs);



It happens behind the seen. when you add the element, the element to be added is compared with the elements already in the list to keep it sorted.

Regards
Padma
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks padma


samura
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic