Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

TreeSet contains and remove not working

809086Oct 28 2010 — edited Oct 29 2010
I am using a TreeSet in a class that extends AbstractListModel. It seems that the TreeSet.remove() and TreeSet.contains() is not finding the object. If I run the code below, the element is not found (sometimes).
public boolean removeElement(VariableInfo element){
        boolean removed = model.remove(element);
        if(removed){
            fireContentsChanged(this, 0, getSize());
        }
        return removed;
}
If I run this code, however, the element is found. (The code below will fail if the uncommented line is used.) Any ideas why this is happening? I don't want to use the code below because it requires iterating over all the elements in the list.
public boolean removeElement(VariableInfo element){
        for(VariableInfo v : model){
            if(v.equals(element)){
                return model.remove(v);
                //return model.remove(element); //will also fail if using this line instead of previous one
            }
        }
        return false;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 26 2010
Added on Oct 28 2010
13 comments
1,847 views