This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Problem with priority Queue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with priority Queue" Watch "Problem with priority Queue" New topic
Author

Problem with priority Queue

Santhosh Gowd
Greenhorn

Joined: Nov 28, 2011
Posts: 13
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.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3575
    
  14

I am assuming you're printing the elements using an enhanced for loop. Do you know how the enhanced 'for' works?
Santhosh Gowd
Greenhorn

Joined: Nov 28, 2011
Posts: 13
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
Bartender

Joined: Sep 20, 2010
Posts: 3575
    
  14

Well, how do you know how the items are stored?
Santhosh Gowd
Greenhorn

Joined: Nov 28, 2011
Posts: 13
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
Bartender

Joined: Sep 20, 2010
Posts: 3575
    
  14

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

Joined: Nov 28, 2011
Posts: 13
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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().


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with priority Queue
 
Similar Threads
Sorting two dimensional array
Array in reverse order......?
Finding Nth Largest element of an array without sorting
Modifying PriorityQueue keys while in queue
adding two arrays