Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using the PriorityQueue class

 
Harold Ndou
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
I meant to say Roel not Noel.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Many thanks Roel

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