4

I'm initialising a Priority Queue like:

strategy = new FuelPriority();
incoming = new PriorityQueue<Vehicle>(1, strategy);

The code for my Comparator class is:

public class FuelPriority implements Comparator<Object> {

public int compare(Object o1, Object o2) {

    Vehicle a1 = (Vehicle) o1;
    Vehicle a2 = (Vheicle) o2;

    return Integer.compare(a1.getFuelLevel(), a2.getFuelLevel());
  }
}

After running a simulation, the elements aren't ordered at all - they are random; I set a breakpoint in the compare method of my FuelPriority class, but it wasn't called at all. Am I missing something here?

2 Answers 2

3

Aside from the typo on your code, it works for me.

import java.util.Comparator;
import java.util.PriorityQueue;

public class StackOverflow
{
    public static void main(String[] args)
    {

        FuelPriority strategy = new FuelPriority();
        PriorityQueue<Vehicle> incoming = new PriorityQueue<Vehicle>(4, strategy);
        incoming.add(new Vehicle("car1", 10));
        incoming.add(new Vehicle("car2", 20));
        incoming.add(new Vehicle("car3", 15));
        incoming.add(new Vehicle("car4", 1));

        // to retrieve the elements in order
        while (!incoming.isEmpty()) {
            System.out.println(incoming.poll());
        }

    }

}

class FuelPriority
    implements Comparator<Object>
{

    public int compare(Object o1, Object o2)
    {

        Vehicle a1 = (Vehicle)o1;
        Vehicle a2 = (Vehicle)o2;

        return Integer.compare(a1.getFuelLevel(), a2.getFuelLevel());
    }
}

class Vehicle
{

    private String name;
    private int fuelLevel;

    public Vehicle(String name, int fuelLevel)
    {
        this.name = name;
        this.fuelLevel = fuelLevel;
    }
    public int getFuelLevel()
    {
        return fuelLevel;
    }

    @Override
    public String toString()
    {
        return name + "=" + fuelLevel;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

It seems to be ordering, but not completely/correctly, here is the output of a run: 13, 16, 27, 21, 19, 39, 37, 50, 30, 34, 64, 46, 49, 72, 59, 58, 55, 61, 47, 73. I've tried it on a few different seeds, and the lowest fuel level is always at the front of the queue, but I don't know why the rest of the objects aren't ordered correctly.
If you want to retrieve the values in sorted order, use poll or peek. Specifically, change the System.out.println(incoming) with while (!incoming.isEmpty()) { System.out.println(incoming.poll()); }
@AlexandreSantos can you take a look at my use of PriorityQueue in this question? stackoverflow.com/questions/28800287/…
2

API says that PriorityQueue iterator is not guaranteed to traverse the elements of the priority queue in any particular order. It's only guaranteed that poll, remove, peek, and element access the element at the head of the queue (least element)

1 Comment

Can you take a look at my use of PriorityQueue in this question? stackoverflow.com/questions/28800287/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.