• 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

Print contents of PriorityQueue not working

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


 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want to peek or poll ?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want it to peek, i.e. just look at all the contents, i.e. 2 4.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But unless you poll (i.e. remove an object from the queue), you'll always get the same object every time you call peek...
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Abimaran: I'll definitely create my own comparator once I understand why the code is modifying PriorityQueue
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ordering of String literals are based on natural(alphabetically). Try the below code

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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.
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the documentation on PriorityQueue's iterator() method.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search in the forum for "PriorityQueue toString", you'll find a lot of previous discussions about it...
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is the part that iterates and modifies the collection
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic