Using an instance of an object as a key in hashmap, and then access it with exactly new object? [duplicate]

You need to implement hashCode and equals on Key. The default implementation of these methods simply checks for instance equality (in other words, two Objects will only be equal if they are in fact the same object).

Further reading

Effective Java – Methods common to all objects

Leave a Comment