• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using the PriorityQueue class

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the PriorityQueue class


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

Hi

Can anyone assist me in understanding how did we get these outcomes.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which outcome(s) don't you understand? Did you have a look at the API of PriorityQueue? Because that's of course the 1st thing to do if you want to understand a code snippet and its output. The code snippet itself uses no rocket science at all, is simply using the API. So using the API it should really be a no-brainer to understand the output.

Good luck!
 
Harold Ndou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Noel
I checked the api, now i got better understanding. Am stil new to java and my other question is
Why system.out.println(pq1.poll() + " ") produce 1 3 5 6 7 8 9, i understand that method poll() retrieve head element and remove it from the queue, i dnt understand why it did not remove head. While in the
System.out.print(poll " + pq2.size()) outcome, head of element is retrieved and removed.

And other thing is that system.out.print(pq2.pol() + " ") produce 8 7 6 5 3 1 null, i dont understand the null at the end.
I will greatly appreciate your assistance.
 
Harold Ndou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant to say Roel not Noel.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the API of poll-method of PriorityQueue-class: Retrieves and removes the head of this queue, or returns null if this queue is empty.

Harold Ndou wrote:Why system.out.println(pq1.poll() + " ") produce 1 3 5 6 7 8 9, i understand that method poll() retrieve head element and remove it from the queue, i dnt understand why it did not remove head.


So your understanding is correct: when using poll the head of the queue will be retrieved and removed. Why do you think the head was not removed? Because with the current code you can't know if the head was removed or not. Try adding System.out.println("size " + pq1.size()); after line 16 (in the above code snippet). And before running the updated code snippet, you should first try to think yourself about the expected output of this new line.

Harold Ndou wrote:And other thing is that system.out.print(pq2.pol() + " ") produce 8 7 6 5 3 1 null, i dont understand the null at the end.


Have another close look at the 1st line of this post and you'll definitely know why null was printed.
 
Harold Ndou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks Roel

Now i understand the out comes of these codes .
reply
    Bookmark Topic Watch Topic
  • New Topic