What is the implementation type of the set and what objects are inside the set?
- If it is a HashSet, make sure that the value of the object's hashCode() method remains constant between
set.put(...)
and set.remove(...)
.
- If it is a TreeSet, make sure that not modifications were made to the object that affect the set's comparator or the object's
compareTo
method.
In both cases, the code between set.put(...)
and set.remove(...)
violates the contract defined by the respective class implementation. As a rule of thumb, it is a good idea to use immutable objects as set content (and as Map keys). By their very nature such objects cannot be changed while they are stored inside a set.
If you are using some other set implementation, check out its JavaDoc for its contract; but usually either equals
or hashCode
must remain the same while the object is contained in the set.