Generics and associative array of keys and diverse objects

Discussion in 'Java' started by Frank Fredstone, Sep 19, 2006.

  1. What is the right way to create a map from Strings to arbitrary and
    diverse objects? For example, so I can have:

    map.put("a", "text");
    map.put("b", new Integer(2));

    etc. where the value needs to be able to be any sub-class of Object.


    I read in a generics FAQ:

    http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

    about creating a collection containing objects like Pair<K, ?>, and
    presumably I could implement a class that behaves like a Map but uses
    a collection of pairs with an unbounded value, but I would think this
    would be a pretty common thing and there might be an easier way to do
    that. Ideas?
     
    Frank Fredstone, Sep 19, 2006
    #1
    1. Advertising

  2. Frank Fredstone schrieb:
    > What is the right way to create a map from Strings to arbitrary and
    > diverse objects? For example, so I can have:
    >
    > map.put("a", "text");
    > map.put("b", new Integer(2));
    >
    > etc. where the value needs to be able to be any sub-class of Object.
    >
    >
    > I read in a generics FAQ:
    >
    > http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html
    >
    > about creating a collection containing objects like Pair<K, ?>, and
    > presumably I could implement a class that behaves like a Map but uses
    > a collection of pairs with an unbounded value, but I would think this
    > would be a pretty common thing and there might be an easier way to do
    > that. Ideas?
    >


    How about Map<String, Object> ?
    This is applicable, if Object is the least common type your value
    objects inherit from. As seen in your example, Object is your choice.

    Tobi
     
    =?ISO-8859-1?Q?Tobias_Schr=F6er?=, Sep 19, 2006
    #2
    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. Juergen Berchtel
    Replies:
    1
    Views:
    6,071
    John C. Bollinger
    May 20, 2005
  2. desktop
    Replies:
    5
    Views:
    401
    James Kanze
    Jun 26, 2007
  3. Miner Jeff

    combining diverse data types

    Miner Jeff, Mar 29, 2009, in forum: C++
    Replies:
    2
    Views:
    317
    Miner Jeff
    Mar 30, 2009
  4. Replies:
    5
    Views:
    165
    Randy Webb
    Jun 21, 2005
  5. laredotornado

    How do I get keys from an associative array?

    laredotornado, Feb 21, 2010, in forum: Javascript
    Replies:
    5
    Views:
    121
    Dr J R Stockton
    Feb 25, 2010
Loading...

Share This Page