JDK-6207984 : (coll) PriorityQueue.remove(Object o) will remove an object with the same priority as o
  • Type: Bug
  • Status: Closed
  • Resolution: Fixed
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Priority: P3
  • Affected Version: 5.0
  • OS: linux
  • CPU: x86
  • Submit Date: 2004-12-14
  • Updated Date: 2012-10-08
  • Resolved Date: 2005-09-04
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
JDK 6
6 b51Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Linux raw 2.6.9 #1 Fri Oct 22 11:25:22 PDT 2004 i686 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
PriorityQueue.remove(Object o) will remove an object with the same priority
as o.  If it does not remove o, then that object is lost to the user, since remove does not return the object that was removed.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
The test code is supplied below.  I define a class that implements Comparable.
I insert one object into a PriorityQueue.  I attempt to remove an object that was not inserted into the queue, but has the same priority.  PriorityQueue removes the other object.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
PQueue size = 1
PQueue size = 1

ACTUAL -
PQueue size = 1
PQueue size = 0

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.PriorityQueue;

public class PQueueTest implements Comparable {
  public int compareTo(Object o) {
    return 0;
  }

  public static void main(String[] args) {
    PriorityQueue<PQueueTest> queue = new PriorityQueue<PQueueTest>();
    
    PQueueTest o1 = new PQueueTest();
    PQueueTest o2 = new PQueueTest();
    queue.add(o1);
    System.out.println("PQueue size = " + queue.size());
    queue.remove(o2);
    System.out.println("PQueue size = " + queue.size());
  }
}


---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I redefined compareTo so that it returns 0 only if the two objects are the same
object.
###@###.### 2004-12-14 00:56:47 GMT

Comments
EVALUATION The semantics of remove(Object) are clearly based on some notion of equality, as specified in Collection.remove(Object). To use the priority comparator for equality is clearly wrong. The bug I just filed in 6268068: (coll) PriorityQueue.remove(Object) should use equals, not its ordering, to find element to remove is more serious than I originally thought. ###@###.### 2005-05-10 19:23:58 GMT
2005-05-10