How to have variables with dynamic data types in Java?
Posted
by Nazgulled
on Stack Overflow
See other posts from Stack Overflow
or by Nazgulled
Published on 2010-05-21T12:41:48Z
Indexed on
2010/05/21
15:01 UTC
Read the original article
Hit count: 172
Hi,
I need to have a UserProfile
class that it's just that, a user profile. This user profile has some vital user data of course, but it also needs to have lists of messages sent from the user friends.
I need to save these messages in LinkedList
, ArrayList
, HashMap
and TreeMap
. But only one at a time and not duplicate the message for each data structure. Basically, something like a dynamic variable type where I could pick the data type for the messages.
Is this, somehow, possible in Java? Or my best approach is something like this? I mean, have 2 different classes (for the user profile), one where I host the messages as Map<K,V>
(and then I use HashMap
and TreeMap
where appropriately) and another class where I host them as List<E>
(and then I use LinkedList
and ArrayList
where appropriately). And probably use a super class for the UserProfile
so I don't have to duplicate variables and methods for fields like data, age, address, etc...
Any thoughts?
© Stack Overflow or respective owner