This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Problem with priority Queue

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using PriorityQueue but it is not giving me exact sorting. This is my code:

But this is sorting this queue of arrays

2 2
1 1
4 3
10 1
2 1

as

1 1
2 1
4 3
10 1
2 2

instead of

1 1
2 1
2 2
4 3
10 1

The same comparator works fine over List sorting. I am not getting whats wrong with PriorityQueue.
 
Saloon Keeper
Posts: 10423
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am assuming you're printing the elements using an enhanced for loop. Do you know how the enhanced 'for' works?
 
Santhosh Gowd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is while storing into the priorityqueue, not while printing. I am assuming that while storing into the queue, it stores in a sorted manner.
 
Stephan van Hulst
Saloon Keeper
Posts: 10423
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, how do you know how the items are stored?
 
Santhosh Gowd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaDoc says about PriorityQueue:
[javadoc]
public class PriorityQueue<E>
extends AbstractQueue<E>
implements Serializable

An unbounded priority queue based on a priority heap. "The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used". A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException). [/javadoc]

Is my constructor wrong or my assumption about Queue construction is wrong?
 
Stephan van Hulst
Saloon Keeper
Posts: 10423
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the way you're retrieving data from the queue is wrong. I already told you that you're accessing the elements from the queue using an enhanced for loop, and I asked you whether you know how the enhanced for loop works.
 
Santhosh Gowd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have modified the code like this to use enhanced for loop:


Still the output is same. How does the enhanced for loop works? Previously i was using iterator.
 
Sheriff
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the same Javadoc page:

The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order.


The same is true for both toArray methods. The only methods that will respect the order are peek() and poll().
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!