Generics and Map?

Discussion in 'Java' started by Remon van Vliet, May 9, 2006.

  1. 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, May 9, 2006
    #1
    1. Advertising

  2. "Remon van Vliet" <> wrote in message
    news:4460764f$0$31654$4all.nl...
    > 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.
     
    Remon van Vliet, May 9, 2006
    #2
    1. Advertising

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

    Remon van Vliet schreef:
    > "Remon van Vliet" <> wrote in message
    > news:4460764f$0$31654$4all.nl...
    >> 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.


    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-----
     
    Hendrik Maryns, May 9, 2006
    #3
  4. "Hendrik Maryns" <> wrote in message
    news:e3qn5c$kjo$-tuebingen.de...
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Remon van Vliet schreef:
    >> "Remon van Vliet" <> wrote in message
    >> news:4460764f$0$31654$4all.nl...
    >>> 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.

    >
    > 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
     
    Remon van Vliet, May 9, 2006
    #4
  5. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Remon van Vliet schreef:
    > "Hendrik Maryns" <> wrote in message
    > news:e3qn5c$kjo$-tuebingen.de...
    >> -----BEGIN PGP SIGNED MESSAGE-----
    >> Hash: SHA1
    >>
    >> Remon van Vliet schreef:
    >>> "Remon van Vliet" <> wrote in message
    >>> news:4460764f$0$31654$4all.nl...
    >>>> 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.

    >> 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.
    >
    > 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:
    >>
    >> 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
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)

    iD8DBQFEYcV2e+7xMGD3itQRAuIXAJ9POkA4QQrOjfXBcGHT3A8yzyfyAACfT+YN
    iyomoB3E4gDjk9BWycrCvZY=
    =Cyt4
    -----END PGP SIGNATURE-----
     
    Hendrik Maryns, May 10, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    10
    Views:
    5,072
    Tony Morris
    Feb 8, 2005
  2. Juergen Berchtel
    Replies:
    1
    Views:
    6,095
    John C. Bollinger
    May 20, 2005
  3. kl
    Replies:
    7
    Views:
    1,345
    James Kanze
    Jan 1, 2008
  4. Ravi

    Generics on map.

    Ravi, Mar 14, 2008, in forum: Java
    Replies:
    10
    Views:
    660
    Roedy Green
    Mar 29, 2008
  5. Soul
    Replies:
    0
    Views:
    550
Loading...

Share This Page