The PriorityVertex
from this example is just a class that the asker created. It is a simple implementation of a vertex in a graph.
This class implements the Comparable
interface. The reason for that is that PriorityQueue
must compare its elements. This can be achieved in two ways:
- The elements must either implement the
Comparable
interface - or a
Comparator
has to be passed to thePriorityQueue
, that tells the queue how to compare the elements.
So the PriorityVertex
class could roughly look as follows:
class PriorityVertex implements Comparable<PriorityVertex>
{
private float priority;
// Setters, getters ... whatever the vertex needs
...
// Implementation of the Comparable interface:
@Override
public int compareTo(PriorityVertex other)
{
return Float.compare(this.priority, other.priority);
}
}