This discussion is archived
2 Replies Latest reply: Nov 5, 2011 6:46 PM by EJP RSS

Behaviour of PriorityQueue using Java 6

652295 Newbie
Currently Being Moderated
Hi,

I have a below code to test PriorityQueue ordering of elements using it's natural order.

Output of SOP1 is clear to me.

I was expecting
SOP2 as : [a, b, d]
SOP3 as : [a, b, c, d]

import java.util.*;
class PQTest {
public static void main(String... args) {
PriorityQueue<String> pq = new PriorityQueue<String>();
     
     pq.add("b");
     pq.add("d");
     System.out.println(pq); //SOP1 output [b, d]
     
     pq.add("a");
     System.out.println(pq); //SOP2 output [a, d, b]
     
     pq.add("c");
     System.out.println(pq); //SOP3 output [a, c, b, d]
}
}

Could somebody shed the light?
Appreciate your help.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points