• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

PriorityQueue

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Why in this code, the result is not [1,2,3] instead of [1,3,2]?

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Can anyone Please answer to the above question?.Since this priority queue holds Integers and its been ordered in natural order,the output should be[1,2,3] but instead the output is [1,3,2]?
 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe is because ProrityQueue does not keep the collection sorted, but, when we retrieve the elements, then these elements are retrieve sorted??
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And maybe the first element is always sorted to avoid a overhead when we manipulate it for first time (peek,remove,pool and element) always get the first element.
 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I think you are wrong Mr.Mateus Brum .

The original question was:---

Why in this code, the result is not [1,2,3] instead of [1,3,2]?


code:
--------------------------------------------------------------------------------

Queue<Integer> queue= new PriorityQueue<Integer>(); queue.offer(3)
;queue.offer(2);
queue.offer(1);
System.out.println(queue);




You are saying that first element is considered sorted.Then according to you answer should be 15234 for below question but answer is 1245.



Queue<Integer> queue= new PriorityQueue<Integer>();
queue.offer(5);
queue.offer(4);
//queue.offer(3);
queue.offer(2);
queue.offer(1);
System.out.println(queue);



Please anybodoy solve the original problem that is top-most.Why the output is not 123 .
[ January 06, 2008: Message edited by: pradeep singh ]
 
Thirumalai Muthu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi can anyone please give answer to the above question. Why is the output [1,3,2] where we are expecting it as [1,2,3]. Thanks.
[ January 07, 2008: Message edited by: Thirumalai Muthu ]
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(yanked from javadocs)
The Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray())



The toString() method that is used when you print an Object using System.out.println or any print method, uses the iterator returned by the PriorityQueue. Since, the iterator does not guarantee an order consistent with the Comparable so the output is not what you would otherwise expect.
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to all
I am not satisfied with Nitesh Kant solution because PriorityQueue orders its elements according to natural order i.e. 1 comes before 2 then 3... unless and untill we do not provide any ordering using Comparator.

And hence in this question we are not using Comparator .So ordering should be in natural order.

So i think(my personel view,may be i am wrong ) solution provide for above problem by Nitesh Kant is wrong.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pradeep singh:
Hi to all
I am not satisfied with Nitesh Kant solution because PriorityQueue orders its elements according to natural order i.e. 1 comes before 2 then 3... unless and untill we do not provide any ordering using Comparator.

And hence in this question we are not using Comparator .So ordering should be in natural order.

So i think(my personel view,may be i am wrong ) solution provide for above problem by Nitesh Kant is wrong.



Could you please let me know as to what you found was wrong in the explanation. I am sure open to anything i missed.
[ January 07, 2008: Message edited by: Nitesh Kant ]
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The answer is bang on target. When System.out.println(queue) is invoked, the println() method simply invokes queue.toString(); And this invokes toString() of AbstractCollection. Because PriorityQueue extends AbstractQueue and AbstractQueue extends AbstractCollection.

AbstractCollection.toString() :"Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator"

AbstractCollection.toString() method invokes iterator(), this method is defined in PriorityQueue and the java doc clearly states that the elements returned by iterator are not in any order. "Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order."

Hope this clears
 
reply
    Bookmark Topic Watch Topic
  • New Topic