Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generics and associative array of keys and diverse objects

Reply
Thread Tools

Generics and associative array of keys and diverse objects

 
 
Frank Fredstone
Guest
Posts: n/a
 
      09-19-2006
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/Generi...nericsFAQ.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?

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Tobias_Schr=F6er?=
Guest
Posts: n/a
 
      09-19-2006
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/Generi...nericsFAQ.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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I get keys from an associative array? laredotornado Javascript 5 02-25-2010 08:36 PM
FA diverse DVDs and SACD-CDs for sale on Ebay anthony DVD Video 2 10-20-2007 03:06 AM
Why "associative" in associative container? desktop C++ 5 06-26-2007 07:49 AM
sorting an associative array keys based on values soup_or_power@yahoo.com Javascript 5 06-21-2005 01:05 AM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM



Advertisments