United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6207984 (coll) PriorityQueue.remove(Object o) will remove an object with the same priority as o
JDK-6207984 : (coll) PriorityQueue.remove(Object o) will remove an object with the same priority as o

Details
Type:
Bug
Submit Date:
2004-12-14
Status:
Closed
Updated Date:
2012-10-08
Project Name:
JDK
Resolved Date:
2005-09-04
Component:
core-libs
OS:
linux
Sub-Component:
java.util:collections
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:

Related Reports
Relates:

Sub Tasks

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



Hardware and Software, Engineered to Work Together