• Post Reply Bookmark Topic Watch Topic
  • New Topic

PriorityQueue giving "weird" results.  RSS feed

 
martin mucha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I just want to ask, whether this is normal or not. I just dont understand this.

final PriorityQueue<Integer> list = new PriorityQueue<>(Arrays.asList(2, 1, 3, 5, 4));
System.out.println(Arrays.toString(list.toArray()));

returns

[1, 2, 3, 5, 4]

Same happens, when I create queue and print result in less compact style, so this is not cause of this result.
I would call this a bug, since I hardly find this respecting natural ordering. Or is there something else I do not understand?
Thanks in advance.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API documentation for the toArray() method of PriorityQueue:

Returns an array containing all of the elements in this queue. The elements are in no particular order.


So yes, that is normal.
 
martin mucha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does not answer the question. I've stated, that it works in this weird way even if we remove all compacting sugar. So here is code rewriten not to use toArray() function.



the output is still



why's that?

same will occur, when we fill the queue in this way:



causing there is NO excuse for bad result. Or is there some explanation?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
martin mucha wrote:
final PriorityQueue<Integer> list = new PriorityQueue<>(Arrays.asList(2, 1, 3, 5, 4));
System.out.println(Arrays.toString(list.toArray()));

here you are converting list to array Object[] and printing, so *Object* is getting printed by Arrays#toString

in recent case explicitly you are looping an list and printed *each* elements.
 
martin mucha
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the answers to everybody...

I've been deceived.

1. toArray works well on List (preserves order) but not on Queues.
2. one would expect, that iterator on PriorityQueue preserves order (otherwise is mostly useless) but

/**
* Returns an iterator over the elements in this queue. The iterator
* does not return the elements in any particular order.
*
* @return an iterator over the elements in this queue
*/

so, it's really ok. Sorry for stupid question.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome
martin mucha wrote:Sorry for stupid question.

there is no such a question as stupid.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To shed more light, here are some thoughts. It's probable that PriorityQueue uses a heap structure internally. This makes removing elements in order very fast, and is more efficient than keeping the data completely sorted at all times. However, iterating over all elements would become very inefficient if the queue was required to sort all the elements first.

For more info, check http://en.wikipedia.org/wiki/Heap_%28data_structure%29
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!