Search code examples
javalinkedhashset

Why isn't my Java LinkedHashSet removing an object it contains?


I have an object in a LinkedHashSet that implements equals, hashCode and compareTo (in a superclass) but when I try to remove that exact object from the set set.remove(obj) the remove method returns false and the object remains in the set. Is the implementation of LinkedHashSet supposed to call the equals() method of its objects? Because it doesn't. Could this be a java bug? I'm running 1.6.0_25.


Solution

  • My guess would be that your object's hashCode() implementation is returning a different value than when you added the object to the set.