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