I just noticed a strange thing that I never paid attention to. java.util.Map
is generic, parameterized with K, V
. For some reason, part of the methods take Object
as an argument, when I would expect it to be either K
or V
.
For example, put method is defined as expected: V put(K key, V value)
. But get and remove methods are not: V get(Object key)
and V remove(Object key)
. Both of them have a note that ClassCastException
will be thrown when key cannot be cast to K
.
The same goes for Multimap
in guava library, where I actually noticed this.
Can someone please explain me the reasons these methods take Object
instead of K
or V
?