Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7030899

(coll) TreeSet uses the wrong method for equality detection during add(E)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • 8
    • 6u24, 7
    • core-libs

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html#contains%28java.lang.Object%29 states that add(E) uses the equals method to determine whether two objects were equal. However, I have determined that it only uses compareTo, which does not make sense in this context. Originally, I had a compareTo that was inconsistent with the equals method because I was only using it to sort elements by a unique identifier. I put a print statement at the beginning of the equals method and it printed nothing for the 500+ times the add method for TreeSet had been run. Adding the following line to the compareTo method fixed this problem and made the TreeSet detect duplicates correctly: if (this.equals((Clause) o)) return 0;


      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      make compareTo consistent with equals

            mduigou Mike Duigou
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: