• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

priorityqueue class

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks padma


samura
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic