This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes Using the 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 » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Using the PriorityQueue class" Watch "Using the PriorityQueue class" New topic
Author

Using the PriorityQueue class

Harold Ndou
Ranch Hand

Joined: May 19, 2013
Posts: 34
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
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

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!


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Harold Ndou
Ranch Hand

Joined: May 19, 2013
Posts: 34
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

Joined: May 19, 2013
Posts: 34
I meant to say Roel not Noel.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

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

Joined: May 19, 2013
Posts: 34
Many thanks Roel

Now i understand the out comes of these codes .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using the PriorityQueue class
 
Similar Threads
Priority queue
Issue in Priority Queue
Generics
Regarding PriorityQueue
priorityqueue class