Why isnt the output in ascending order?
public class Test {
public static void main(String[] args) {
PriorityQueue<Edge> edges = new PriorityQueue<Edge>();
edges.add(new Edge(1, 2, 23));
edges.add(new Edge(2, 3, 1000));
edges.add(new Edge(1, 3, 43));
Iterator<Edge> i = edges.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
class Edge implements Comparable<Edge> {
private int v1;
private int v2;
private int w;
Edge(int v1, int v2, int w) {
this.v1 = v1;
this.v2 = v2;
this.w = w;
}
public int getV1() {
return v1;
}
public int getV2() {
return v2;
}
public int getW() {
return w;
}
@Override
public int compareTo(Edge o) {
return this.w - o.getW();
}
public String toString() {
return ("v1: " + v1 + " v2: " + v2 + " w: " + w);
}
}
I tried it using doing this using a List, and then calling Collections.sort(listToSort) and it works. I thought the head of the PriorityQueue is always the least element?