Generics and Map?

  • Thread starter Remon van Vliet
  • Start date
R

Remon van Vliet

There's something i've been wondering and i'm hoping someone has an
explanation for it that goes beyond "Sun screwed up" :

The interface Map<K,V> (with generics) has these methods defined :

public V get(Object key)
public void put(K key, V value)

Now, what puzzles me is why get is defined like that rather than :

public V get(K key)

In other words, it seems to ignore the generic type defined for the key.
Does anyone have an explanation for this? The same applies to the remove
method by the way.
 
R

Remon van Vliet

Remon van Vliet said:
There's something i've been wondering and i'm hoping someone has an
explanation for it that goes beyond "Sun screwed up" :

The interface Map<K,V> (with generics) has these methods defined :

public V get(Object key)
public void put(K key, V value)

Now, what puzzles me is why get is defined like that rather than :

public V get(K key)

In other words, it seems to ignore the generic type defined for the key.
Does anyone have an explanation for this? The same applies to the remove
method by the way.

I already found my answer, never mind.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Remon van Vliet schreef:
I already found my answer, never mind.

How about you share it with the people that tried to come up with an
answer, but gave up?

H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEYN/te+7xMGD3itQRAv9hAJ4u02ac5vJdeL3Z7jJD9mqCvQKbNwCfYmZ1
x3q3Jy9395f9Ckmr+Xef6Zw=
=mSLq
-----END PGP SIGNATURE-----
 
R

Remon van Vliet

Hendrik Maryns said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Remon van Vliet schreef:

How about you share it with the people that tried to come up with an
answer, but gave up?

H.

- --
Hendrik Maryns

Hehe, a valid point sir, it's rather rude to ask a question and then not
post the answer, so my apologies.

A possible explanation is as follows :

If the method signature of Map.get would be get(K key) there is no way to
pass anything but an object of type K to this method at run-time because the
code wouldnt compile (generics, after all, is a compile time wizardry). In
most circumstances this is actually a good thing, but sometimes you want to
check if there's a value in your map with key object X without really caring
(or wanting to care) if that object X is actually of type K. If the method
signature was as i suggested this code wouldnt compile, and would thus force
an explicit instanceof check or checked cast at all points where you use
get/containsKey/etc. methods.

Now i should point out that it probably could be argued that such strictness
is actually a good thing since it reduces the possibility of type bugs, but
i guess the guys at Sun opted for flexibility.

As far as i'm aware changing the method signatures to include generics does
not break legacy code, so that shouldnt be the reason. Also, methods like
AbstractMap.equals(Object o) could still be implemented since a Map<K, V> t
= (Map<K, V>)o; is allowed even if o is not of Map<K, V>. I'm having a
discussion on the Java forums regarding the reason last cited, a lot of
people believe it is mostly due to that reason that Objects rather than Ks
are used in such method signatures.

If i'm missing something please let me know, but currently i'm just assuming
it's simply flexibility.

- Remon
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Remon van Vliet schreef:
Hehe, a valid point sir, it's rather rude to ask a question and then not
post the answer, so my apologies.

A possible explanation is as follows :

If the method signature of Map.get would be get(K key) there is no way to
pass anything but an object of type K to this method at run-time because the
code wouldnt compile (generics, after all, is a compile time wizardry). In
most circumstances this is actually a good thing, but sometimes you want to
check if there's a value in your map with key object X without really caring
(or wanting to care) if that object X is actually of type K. If the method
signature was as i suggested this code wouldnt compile, and would thus force
an explicit instanceof check or checked cast at all points where you use
get/containsKey/etc. methods.

Now i should point out that it probably could be argued that such strictness
is actually a good thing since it reduces the possibility of type bugs, but
i guess the guys at Sun opted for flexibility.

As far as i'm aware changing the method signatures to include generics does
not break legacy code, so that shouldnt be the reason. Also, methods like
AbstractMap.equals(Object o) could still be implemented since a Map<K, V> t
= (Map<K, V>)o; is allowed even if o is not of Map<K, V>. I'm having a
discussion on the Java forums regarding the reason last cited, a lot of
people believe it is mostly due to that reason that Objects rather than Ks
are used in such method signatures.

If i'm missing something please let me know, but currently i'm just assuming
it's simply flexibility.

I think this is also to let new code be able to interoperate with legacy
code: legacy code will always give an Object to Map.get, there might be
cases where this will lead to compile problems.

H.

P.S. If you want to get rid of this little bug in Thunderbird:then go to https://bugzilla.mozilla.org/show_bug.cgi?id=99922 and vote
for the bug.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEYcV2e+7xMGD3itQRAuIXAJ9POkA4QQrOjfXBcGHT3A8yzyfyAACfT+YN
iyomoB3E4gDjk9BWycrCvZY=
=Cyt4
-----END PGP SIGNATURE-----
 

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

Similar Threads

generics puzzle 57
Delegation and generics craziness 9
Queue and Map interface 4
Generics for a multiplevalue hashmap 8
Generic generics help 7
Generics 12
Generics and Polymorphism 5
Generics and for each 12

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top