What is the type of implementation of the collection and what objects are inside the collection?
- If it is a HashSet, make sure that the value of the hashCode () method of the object remains constant between
set.put(...) and set.remove(...) . - If it is a TreeSet, make sure that no changes have been made to the object that affects the installed comparator or method of the
compareTo object.
In both cases, the code between set.put(...) and set.remove(...) violates the contract defined by the corresponding implementation of the class. As a general rule, it is recommended that you use immutable objects as the specified content (and as map keys). By their very nature, such objects cannot be modified while they are stored inside a set.
If you use any other set implementation, check its JavaDoc for your contract; but usually equals or hashCode should remain unchanged while the object is contained in the set.
source share