PriorityQueue question

• 03-10-2011, 02:37 AM
kryptonian03
PriorityQueue question
Good day!

Im just wondering, why does my priority queue elements doesnt sort?
Code:

```import java.util.*; public class PriorityQueueDemo { public static void main(String[] args){ Queue<String> s = new PriorityQueue<String>(); Queue<Integer> u = new PriorityQueue<Integer>();                s.add("ab"); s.add("abcd"); s.add("abc"); s.add("a"); s.add("b"); u.add(32); u.add(12); u.add(1); //don't use iterator which may or may not //show the PriorityQueue's order //while( s.size() > 0)   //    System.out.println(s.remove());          System.out.println (s);          System.out.println (u);                        } }```
• 03-10-2011, 02:42 AM
Junky
How do you know it doesn't sort?
• 03-10-2011, 03:05 AM
kryptonian03
When you run the code, the output is:
[a, ab, abc, abcd, b]
[1, 32, 12] ------ this one is not sorted
• 03-10-2011, 03:14 AM
Junky
It is pure luck that the first one appears sorted.

Code:

`System.out.println (s);`
Do you understand what that line of code does? It calls the toString method. this is what the API says:
"Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). "

Here is what the API says about the iterator method:
"Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order"
"
• 03-10-2011, 03:15 AM
gcalvin
Quote:

Originally Posted by kryptonian03
When you run the code, the output is:
[a, ab, abc, abcd, b]
[1, 32, 12] ------ this one is not sorted

This output is provided by the toString() method of the PriorityQueue class. Look that up in the API docs, and keep this in mind:

Quote:

//don't use iterator which may or may not
//show the PriorityQueue's order
-Gary-
• 03-10-2011, 03:16 AM
doWhile
And what about the toString method makes you think it will print out the elements in order? Did you read the API?
Try
Code:

```System.out.println (u);         while( u.size() > 0)               System.out.println(u.remove());                                                                }```
Edit: not fast enough again...
• 03-10-2011, 03:19 AM
Junky
Change that to poll and not remove.

Ack, never mind I missed the inherited method.
• 03-10-2011, 03:51 AM
kryptonian03
Thanks a lot!

Code:

```import java.util.*; public class PriorityQueueDemo { public static void main(String[] args){ Queue<String> s = new PriorityQueue<String>(); Queue<Integer> u = new PriorityQueue<Integer>();                s.add("ab"); s.add("abcd"); s.add("abc"); s.add("a"); s.add("b"); u.add(32); u.add(12); u.add(1);          System.out.println (s);                  while( u.size() > 0){                       System.out.println(u.poll());                }         System.out.println (u);                                }                        }```
How can remove the [] at output:
[a, ab, abc, abcd, b]
1
12
32
[] ---- this one

Thanks a lot
• 03-10-2011, 04:35 AM
Junky
As we tried to explain, if you just print the Queue it calls the toString method and the square brackets are the default behaviour of that method. If you don't want them then you will have to implement your own printing method.
• 03-10-2011, 04:44 AM
kryptonian03
Now i understand... thats why you have to used print a poll operation according to the size of each data... thank you very much