4

I have a class that uses priority queue to display 5 strings in ascending order. I understand that to make it in descending order i can use the "collections.reverseOrder()" method. How use this method with the following code?

import java.util.*;
public class queue {

    public static void main (String[] args) {

        PriorityQueue<String> sQ = new PriorityQueue<String>();

        sQ.add("theodore");
        sQ.add("theo");
        sQ.add("Shailee");
        sQ.add("Deborah");
        sQ.add("Fernando");
        sQ.add("th");

        while (sQ.size() > 0)
            System.out.println(sQ.remove());

        Collections.reverseOrder(); //I am stuck here...
    }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
choloboy
  • 795
  • 4
  • 16
  • 38

3 Answers3

12

Try something like this, before removing the elements in sQ:

PriorityQueue<String> reversed =
    new PriorityQueue<String>(sQ.size(), new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        return -o1.compareTo(o2);
    }
});
reversed.addAll(sQ); // now `reversed` contains the reversed priority queue

Because you're using the natural ordering of String, it makes sense to just build another PriorityQueue passing as a parameter a new comparator that compares strings but reversing the order (notice the - sign in front of the comparison).

EDIT:

As has been pointed in the comments, this is an even simpler solution:

PriorityQueue<String> reversed =
    new PriorityQueue<String>(sQ.size(), Collections.reverseOrder());
reversed.addAll(sQ);
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Could you tell me what the "o1 an o2" is? – choloboy Apr 09 '12 at 19:39
  • @Oscar You can even make it simpler than that. This should work also: `PriorityQueue reversed = new PriorityQueue( sQ.size(), Collections.reverseOrder() );` – Ryan Nelson Apr 09 '12 at 19:48
  • Each pair of elements in the collection. When the queue is sorting its elements, the comparator gets called for each pair of elements to determine which one should go first – Óscar López Apr 09 '12 at 19:50
  • thanks for your help Oscar and Ryan... Now I created a similar while loop thats in my code above to display the elements but I keep getting an error here... "PriorityQueue reversed = new PriorityQueue(sQ.size(), Collections.reverseOrder());" – choloboy Apr 09 '12 at 19:56
  • @theolc you're welcome!. If this answer was helpful for you, please consider accepting it as correct (click the check mark to its left) – Óscar López Apr 09 '12 at 20:00
  • Exception in thread "main" java.lang.IllegalArgumentException at java.util.PriorityQueue.(Unknown Source) at queue.main(queue.java:19) – choloboy Apr 09 '12 at 21:23
  • @theolc The objects that you're adding to the `PriorityQueue`, are they `String`s ? The second solution (using `reverseOrder()` only works for simple types (strings, integers, etc.) If it's a class you made, you need to use the first solution, creating a comparator explicitly – Óscar López Apr 09 '12 at 21:37
  • I wish to use the same elements as listed in my original code above, therefore yes they are strings. – choloboy Apr 09 '12 at 21:44
  • @theolc try with the first solution. And/or examine the stack trace to see if the error originates elsewhere; otherwise I don't know what could be wrong – Óscar López Apr 09 '12 at 21:51
3

First you're removing the elements from the queue: don't forget to add them back. And if you check out: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#reverseOrder() you'll notice how to use it :)

Best of luck!

1

You can also try this:

    String[] strArray = new String[0];

    /*Creating array from Queue*/
    strArray = sQ.toArray(strArray);

    Arrays.sort(strArray,Collections.reverseOrder());   

    System.out.println("\n Elements of Queue in REVERSE:\n");

    for(String s:strArray)
    System.out.print(s+" ,");

But problem is it will also sort your list in reverse order

a Learner
  • 4,944
  • 10
  • 53
  • 89