Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Generics and Map?

Thread view: 
Remon van Vliet - 09 May 2006 12:00 GMT
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.
Remon van Vliet - 09 May 2006 19:22 GMT
> There's something i've been wondering and i'm hoping someone has an
> explanation for it that goes beyond "Sun screwed up" :
[quoted text clipped - 11 lines]
> Does anyone have an explanation for this? The same applies to the remove
> method by the way.

I already found my answer, never mind.
Hendrik Maryns - 09 May 2006 19:31 GMT
Remon van Vliet schreef:
>> There's something i've been wondering and i'm hoping someone has an
>> explanation for it that goes beyond "Sun screwed up" :
[quoted text clipped - 13 lines]
>
> 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
Remon van Vliet - 09 May 2006 21:06 GMT
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 25 lines]
> - --
> 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
Hendrik Maryns - 10 May 2006 11:50 GMT
Remon van Vliet schreef:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
[quoted text clipped - 19 lines]
>> How about you share it with the people that tried to come up with an
>> answer, but gave up?

> Hehe, a valid point sir, it's rather rude to ask a question and then not
> post the answer, so my apologies.
[quoted text clipped - 25 lines]
> 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:

>> H.
>>
>> - --
>> Hendrik Maryns

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.