I was learning Java and trying to learn priority queue in collections. I tried with below example from one website:
import java.util.*;
class S
{
public static void main(String args[])
{
PriorityQueue<String> queue=new PriorityQueue<String>();
queue.add("Amit");
queue.add("Vijay");
queue.add("Karan");
queue.add("Rahul");
queue.add("Jai");
System.out.println("iterating the queue elements:");
Iterator<String> itr=queue.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
here results came is below:
Amit
Jai
Karan
Vijay
Rahul`
I was expecting the result as :
Amit
Vijay
Karan
Rahul
Jai
I am not able to understand how the result changes from my expectation and what type of normal or default priority is used.
In case i want to get the same result as per my expectation, what should i do using prioiryqueue?
Please help me.
Here i want the exact cause of default ordering in priority queue.