wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes priorityqueue class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "priorityqueue class" Watch "priorityqueue class" New topic
Author

priorityqueue class

aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111

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

Joined: Mar 22, 2007
Posts: 111
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

Joined: Jun 18, 2007
Posts: 383
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.


SCJP-1.5<br />SCWCD-1.4
Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi

I guess you are referring to this.

web page

Regards
Padma
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
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

Joined: Mar 22, 2007
Posts: 111
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

Joined: Mar 03, 2007
Posts: 111
Thanks padma


samura
 
Consider Paul's rocket mass heater.
 
subject: priorityqueue class
 
Similar Threads
Collections..
Issue in Priority Queue
Generics
Regarding PriorityQueue
Priority queue