United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6268068 (coll) PriorityQueue.remove(Object) should use equals, not its ordering, to find element to remove
JDK-6268068 : (coll) PriorityQueue.remove(Object) should use equals, not its ordering, to find element to remove

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

Related Reports
Relates:

Sub Tasks

Description
PriorityQueue.remove(Object) uses its ordering, that is,
either (Comparable)o.compareTo(e), or comparator.compare(o,e).

Instead it should use o.equals(e)... because:

- that's what the base interface Collection.remove(Object) spec says
- the implementation throws ClassCastException, but the spec does not
  mention this exception.
- remove(Object) should be consistent with contains(Object), i.e.
  remove(Object) should be able to find an element to remove iff
  contains(Object) is able to find it.  remove(Object) should be
  implemented in terms of equals iff contains(Object) is.

###@###.### 2005-05-10 17:43:47 GMT

                                    

Comments
EVALUATION

A fine idea.
###@###.### 2005-05-10 17:43:47 GMT
                                     
2005-05-10
SUGGESTED FIX


    private int indexOf(Object o) {
	if (o == null)
	    return -1;
	for (int i = 1; i <= size; i++)
	    if (o.equals(queue[i]))
		return i;
        return -1;
    }
    
    /**
     * Removes a single instance of the specified element from this queue,
     * if it is present.  More formally, removes an element <tt>e</tt> such
     * that <tt>o.equals(e)</tt>, if this queue contains one or more such
     * elements.  Returns true if this queue contained the specified element
     * (or equivalently, if this queue changed as a result of the call).
     *
     * @param o element to be removed from this queue, if present
     * @return <tt>true</tt> if this queue changed as a result of the call
     */
    public boolean remove(Object o) {
	int i = indexOf(o);
	if (i == -1)
	    return false;
	else {
	    removeAt(i);
	    return true;
	}
    }

    /**
     * Returns <tt>true</tt> if this queue contains the specified element.
     * More formally, returns <tt>true</tt> if and only if this queue contains
     * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
     *
     * @param o object to be checked for containment in this queue
     * @return <tt>true</tt> if this queue contains the specified element
     */
    public boolean contains(Object o) {
	return indexOf(o) != -1;
    }

###@###.### 2005-05-10 17:43:47 GMT
                                     
2005-05-10



Hardware and Software, Engineered to Work Together