Can anyone tell me in which order elements of priority queue are sorted. I have used following code class Test{ PriorityQueue <strings> pq=new PriorityQueue <Strings>(); pq.add("silpa"); pq.add("swati"); pq.add("roopa"); pq.add("abc"); System.out.println(pq); System.out.println(pq.poll()); System.out.println(pq.peak()); } The output I got is [abc silpa swathi roopa] abc roopa.
Can anybody explain why the collection elements are not sorted ? I am getting correct output with peek() n poll().According that methods i should get [abc roopa silpa swathi ]
As you said, retrieving all entries with Queue methods gives you the order you expect.
That note about iterator was interesting. I guess they're trying hard to not reveal what their internal storage is like. And toString() in your original print of the queue is inherited from AbstractCollection which doesn't promise order, so no surprise it didn't print in order. [ November 22, 2007: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
a bit of art, as a gift, the permaculture playing cards