What problems can arise if the result of the equals()
and hashCode()
methods changes across the lifetime of an object?
Thank you!
One problem is that you won't be able to find that object in a HashSet or a HashMap (when that object is a key in the Map) if its hashCode
changes after to add it to the Collection.
Changing the result of equals
during the lifetime of an object may result in breaking some Collections. For example, you may find you have duplicate objects in your Set, because at the time the second object was added to the Set, they weren't equal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With