PriorityQueue<
String> pq = new PriorityQueue<String>();
pq.add("2");
pq.add("4");
pq.add("3");
pq.add("1");
pq.add("10");
System.out.println(pq);
result is [1, 10, 3, 4, 2]
I would like to learn why 2 is at the end ?
It does not seem to be naturally ordered if 2 is at the end ?
Moreover if I used like this
PriorityQueue<String> pq = new PriorityQueue<String>();
pq.add("5");
pq.add("2");
pq.add("4");
pq.add("3");
pq.add("1");
System.out.println(pq);
the result is [1, 2, 4, 5, 3]
I am really confused why the result varies like this ?
Help needed?
and also it does not chande if I used Integer instead of String
PriorityQueue<Integer> pqn = new PriorityQueue<Integer>();
pqn.add(5);
pqn.add(2);
pqn.add(4);
pqn.add(3);
pqn.add(1);
System.out.println(pqn);
result is [1, 2, 4, 5, 3]