
Untitled
By: a guest on
May 7th, 2012 | syntax:
None | size: 0.70 KB | hits: 12 | expires: Never
Unexpected behavior with PriorityQueue remove: Why isn't compareTo used?
PriorityQueue<OwnClass> pq=new PriorityQueue<OwnClass>();
OwnClass a=new OwnClass(1);
OwnClass b=new OwnClass(2);
OwnClass c=new OwnClass(3);
pq.add(a);
pq.add(b);
pq.add(c);
System.out.println("head:"+pq.peek());
pq.remove(new OwnClass(1));
System.out.println(pq.peek());
class OwnClass implements Comparable{
int x;
public OwnClass(int x){
this.x=x;
}
public int compareTo(Object arg0) {
OwnClass a=(OwnClass) arg0;
if(a.x>this.x)
return -1;
if(a.x<x)
return 1;
return 0;
}
public String toString(){
return ""+x;
}
}