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 / September 2006

Tip: Looking for answers? Try searching our database.

Generics and associative array of keys and diverse objects

Thread view: 
Frank Fredstone - 19 Sep 2006 12:34 GMT
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?
Tobias Schröer - 19 Sep 2006 12:42 GMT
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:
[quoted text clipped - 13 lines]
> 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


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



©2008 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.