The objects used to retrieve/remove/check for existance of a given key need not necessarily be of the same type as the object used to store it (= the key).
It needs to be equal
and return the same hashCode
as the key, but nothing in the spec says that it must be of the same type.
That fact is rarely used and most of the time you'll retrieve the values with the same keys (or at least objects of the same types) as the ones you use to store them.
But since that was a supported use case in the "old" HashMap
, it needs to be supported in the generics version as well.
Note that all methods that keySet()
uses the specific type, as it's sure to return exactly the objects used as keys when put()
was called.