When a class does not override the equals()
or hashCode()
methods, the default implementations found on the Object
class are used instead. In particular, equals()
simply does a check for reference equality.
That immediately explains why your approach isn't working: the new Key
object clearly isn't referring to the old Key
object.
If you'd like to be able to specify a new instance with the same property, then you should override the equals()
method with an implementation that meets your criteria for key equality. You should override hashCode()
as well, to have full control over the key comparison process.