Guest User

Untitled

a guest
May 7th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. Unexpected behavior with PriorityQueue remove: Why isn't compareTo used?
  2. PriorityQueue<OwnClass> pq=new PriorityQueue<OwnClass>();
  3. OwnClass a=new OwnClass(1);
  4. OwnClass b=new OwnClass(2);
  5. OwnClass c=new OwnClass(3);
  6. pq.add(a);
  7. pq.add(b);
  8. pq.add(c);
  9. System.out.println("head:"+pq.peek());
  10. pq.remove(new OwnClass(1));
  11. System.out.println(pq.peek());
  12.  
  13. class OwnClass implements Comparable{
  14.  
  15. int x;
  16.  
  17. public OwnClass(int x){
  18. this.x=x;
  19. }
  20.  
  21. public int compareTo(Object arg0) {
  22.  
  23. OwnClass a=(OwnClass) arg0;
  24. if(a.x>this.x)
  25. return -1;
  26. if(a.x<x)
  27. return 1;
  28. return 0;
  29. }
  30.  
  31. public String toString(){
  32. return ""+x;
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment