*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Print contents of PriorityQueue not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Print contents of PriorityQueue not working" Watch "Print contents of PriorityQueue not working" New topic
Author

Print contents of PriorityQueue not working

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Variation of questions at end of Chapter 7, Sierra/Bates:



I expect the output to be 2 4; however it is 2 2.

Why is 2 repeated and 4 omitted? I even tried replacing add with offer (not sure of the difference) and still the same output.

Thanks!



Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Do you want to peek or poll ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
I want it to peek, i.e. just look at all the contents, i.e. 2 4.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

But unless you poll (i.e. remove an object from the queue), you'll always get the same object every time you call peek...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

peek() returns the highest priority element without removing it, and poll() returns the highest priority element AND removing it in the Queue. So If you don't remove(poll()) the highest element, you'll get it again and again.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Ok, I changed peek to poll, and now it is printing only 2 as the output:




How do I get it to print 2 4
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

The problem is you are iterating and polling at the same time. Iterate but do not use poll. Or poll until there are no more objects
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

oh by the way... there is another problem with the code. It tried to modify the collection while iterating over it. The problem will not surface unless there are more elements in the collection.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Two part response

I: I modified the code such that 2 4 is now the output




II: I added more elements to the PriorityQueue, and cannot figure out how it is modifying the collection while iterating over it. Please explain (code and output below):



Output:

10 2 12 8 4 6



Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

@ Sandra Bachan :
A PriorityQueue orders its elements using a user-defined priority. It can be natural order(in which, an entry 1 would be a higher priority than entry 2, like in your case). In addition, a PriorityQueue can be ordered using a Comparator, which lets you define any order toy want. Try to use your own Comparator.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
@ Abimaran: I'll definitely create my own comparator once I understand why the code is modifying PriorityQueue
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ordering of String literals are based on natural(alphabetically). Try the below code

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
@ Abimaran: I tried the code, and the output is:

>five<>four<>six<>two<>one<>three<
[five, four, six, two, one, three]
>five<>four<>six<>two<>one<>three<



I would think that alphabetically one would come before six and that three would come before two - please explain these discrepancies.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
See the documentation on PriorityQueue's iterator() method.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Search in the forum for "PriorityQueue toString", you'll find a lot of previous discussions about it...
ravi keshri
Greenhorn

Joined: Jun 30, 2010
Posts: 3
hiiiiiii sandra.
i think you have to know the diffrence between poll() and peek() methd .
peek() method retrive header of the queue and only retrive not remove.
poll() method retrive and remove header of queue
HAVE A GOOD DAY
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5



This is the part that iterates and modifies the collection
Chad Michaels
Ranch Hand

Joined: Jun 25, 2010
Posts: 42
@Sandra

I would think that alphabetically one would come before six and that three would come before two - please explain these discrepancies.


"one" is coming before "six" because you are simply iterating the collection. In order for the PriorityQueue to return elements in their order of highest priority, you should invoke it's "poll()". My guess is, if you run that same code using poll() instead of using an enhanced for-loop or an iterator, the results would be as you expect. For example, "one" will come before "six".

This is straight out of the API for PriorityQueue,
The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order.

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Based on the discussions on this forum, seems that you have to manually poll() or peek() to have queue printed in order, such as the code below (as)




Output:

1 2 3 4 5 6


Ouch!
kavitha yogaraj
Greenhorn

Joined: Apr 21, 2010
Posts: 19
Sandra Bachan wrote:Based on the discussions on this forum, seems that you have to manually poll() or peek() to have queue printed in order, such as the code below (as)




Output:

1 2 3 4 5 6


This output is perfect only when numbers of Integer class or String class ex- "1" ,"8", "4","2" is considered & the putput wud still be orderd removal by poll()
But when PriorityQueue<String> pq ---->"Seventy", "Six ","One", " Two", like this unordered strings are loaded poll wud worka as deisred. I mean the output of using above code would not produce the ordered removal by pq.poll().
hence the question posed by Sandra Bachan is unanawered. As he/she wanted 2 then 4 to removed.

kindly forgive me if I'm wrong, but I too had the same question wherein I feel Priority Queue would not work for String of chars.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

For these question, as Ankit said, search it.

System.out.println is invoking the toString() method of the PriorityQueue, which is using the iterator, which is not guaranteed to respect the natural ordering. From the docs: "The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order."So when you print the PriorityQueue with System.out.println(), you won't get order(natural/yours). If you print them manually as Sandra did, you'll get the order!
kavitha yogaraj
Greenhorn

Joined: Apr 21, 2010
Posts: 19
Abimaran Kugathasan wrote:For these question, as Ankit said, search it.

System.out.println is invoking the toString() method of the PriorityQueue, which is using the iterator, which is not guaranteed to respect the natural ordering. From the docs: "The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order."So when you print the PriorityQueue with System.out.println(), you won't get order(natural/yours). If you print them manually as Sandra did, you'll get the order!



I agree with this about iterator and not guaranteed behaviour of iterator.
but when pq.poll() method is called (with pq containing a string of char with String type safe) its supposed to remove the highest priority element in that priority queue isn'it?

code: printing manually as Sandra did.

public class PQ1{

public static void main(String [] args){
PriorityQueue<String>pq = new PriorityQueue<String>();
// add elements to pq
pq.offer("one");
pq.offer("twenty");
pq.offer("two");
pq.offer("six");
pq.offer("five");
pq.offer("seven");
pq.offer(" eighty");

System.out.println(pq.poll()+" ");
System.out.println(pq.poll()+" ");
System.out.println(pq.poll()+" ");
System.out.println(pq.poll()+" ");
System.out.println(pq.poll()+" ");
System.out.println(pq.poll()+" ");
System.out.println(pq.poll()+" ");
}
}


will always give unordered output.
But in palce of one two, had I placed 1, 2,80,3,7 then the output is ordered just like how a ideal priorotyqueue as to work.


Vicky Mehta
Greenhorn

Joined: Jul 15, 2010
Posts: 15
but when pq.poll() method is called (with pq containing a string of char with String type safe) its supposed to remove the highest priority element in that priority queue isn'it?
will always give unordered output.
But in palce of one two, had I placed 1, 2,80,3,7 then the output is ordered just like how a ideal priorotyqueue as to work

No, output for pq containing a string of char with String type will be as per the natural ordering of strings which is lexicographical ordering(that is their comparison will be based on Unicode value of each character in the strings). Hence, out for the program will be :

Note ' '(space) character before the string eighty


Preparing for SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Print contents of PriorityQueue not working