Java: Modify id that changes hashcode

Viewed 4527

I use HashSet and I need to modify the ID of an object, but it changes hashcode and breaks HashSet and rules of hashCode() method.

What is best solution: to delete object from Set and add object with new ID, or to keep the hash code (generated in constructor, for example) in every object in Set, or is there other way to solve this problem?

Thanks for help.

UPDATE: I made mistake: keeping hash code in object is terrible, because in that case equal objects can have different hash codes.

1 Answers
Related