Bug in API for java.util.Map.get ?

D

Donna

I've noticed that in the jdk 1.5, the argument for java.util.Map.get()
is Object, rather than the type parameter K. Does anyone know why this
is? I sometimes make a type error and pass to get() something that has
a completely different type, and of course it won't be in the map,
since put() requires that the key have the right type.
 
S

Stefan Schulz

I've noticed that in the jdk 1.5, the argument for java.util.Map.get()
is Object, rather than the type parameter K. Does anyone know why this
is? I sometimes make a type error and pass to get() something that has
a completely different type, and of course it won't be in the map,
since put() requires that the key have the right type.

This has legacy reasons. If you put K there, you could no longer pass any
oObject to the map, and fall back to the old default behaviour. (That
being, getting null because the object does not occur in the Map). Some
code might rely upon that (for example, iterating over several maps, with
different key types, and if the map returns something different from null,
do something with the result)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top