File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Priority Queue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Priority Queue" Watch "Priority Queue" New topic
Author

Priority Queue

gunjan khanuja
Ranch Hand

Joined: Apr 16, 2012
Posts: 37
In what order are the elements added to a priority queue?
for example i have this code snippet
import java.util.*;

public class TryMe {
public static void main(String args[]) {
Queue<String> q = new PriorityQueue<String>();
q.add("3");
q.add("1");
q.add("2");
for(String a:q)
{
System.out.println(a);
}
}

it gives me output 132??
Could you please explain if some specific order is followed?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

hi gunjan, i have really no idea what you are talking about ?


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
A PriorityQueue maintains its elements an order that has nothing to do with the order in which they are added. You can supply a Comparator when the PriorityQueue is instantiated; in the absence of a comparator, the queue will use the ordering imposed by the compareTo method.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

hi, generally queues are ordered on the basis of FIFO but it is not always true. for example PriorityQueue. they are ordered on the basis of their natural ordering or on the basis of comparator. this thing depends upon which constructor you used to construct the PriorityQueue. In your case you used the no-arg constructor which orders its elements on the basis of their natural ordering . since the elements are of type Strings their natural ordering is from low to high. so you get the output 132. Please refer the following link http://docs.oracle.com/javase/6/docs/api/. guys correct me wherever i'm wrong.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

And dear gunjan next time whenever you post your question please format your coding stuff in the editor. there is a markup tag provided for code listings write in the editor. Hope you will do the same in the future. And all the best for your exam.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
However, as the JavaDoc indicates, the Iterator of a PriorityQueue returns the elements in no specified order; so we really can't glean anything useful about the data structure by iterating and printing its elements. The output does not necessarily show us how the elements will be returned by normal usage of the queue.
gunjan khanuja
Ranch Hand

Joined: Apr 16, 2012
Posts: 37
if low to high is the natural order it should have been 1 2 3
is it checking in terms of strings.. i mean after converting them to 'one two three' and then applying the natural order.??
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
gurpeet singh wrote:since the elements are of type Strings their natural ordering is from low to high. so you get the output 132.


No, that has nothing to do with the output that is seen. If we want to see how the elements of the queue are ordered internally, we can do this:
 
Consider Paul's rocket mass heater.
 
subject: Priority Queue
 
Similar Threads
Ordering in PriorityQueue
another question from scjptest.com
Generics
Doubt on PriorityQueue?
Queue