开发者

How to have variables with dynamic data types in Java?

开发者 https://www.devze.com 2022-12-31 07:58 出处:网络
I need to have a UserProfile clas开发者_如何学编程s 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 th

I need to have a UserProfile clas开发者_如何学编程s 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?


First of all, you are not duplicating a message by adding it to different collections at the same time - you only store distinct references to the same object. (Well, unless a message is represented as a primitive type like long... but these can't be added to collections anyway.)

Why can't you have all those collections within the same UserProfile? This would allow you to access messages by key or index, and iterate through them any way you like.

A LinkedHashMap might also be an interesting option for you, as it guarantees iteration order, so in a way it behaves similarly to a List regarding iteration, while still being a Map. Ultimately, it boils down to how you want to access the messages of a given user, which you haven't detailed.

Update: @Snake, you can only store references to objects in Java collections. A primitive long value thus can't be stored directly, only by converting to a Long object first. Note that since Java5, this conversion may be implicit due to autoboxing, so you don't see it in the code, but it still happens nevertheless - e.g.

List<Long> list = new ArrayList<Long>();
list.add(1L); // the primitive value is boxed into a Long object,
              // which is then added to the list
long value = list.get(0); // the value of the Long object in the list is outboxed
                          // and assigned to the primitive variable


If this is a university project, then I suspect that what you are meant to do is this:

Collection mycoll;
mycoll = new ArrayList();
for (Message m:message) {
  // do stuff and measure the performance
}
// do other stuff and measure the performance
mycoll = new LinkedList();
// do the same stuff as a above and measure the performance again
mycoll = new HashMap();
//... and so on

As stated above, adding an object to a collection doesn't duplicate it.


I ended up using what I described on the first post:

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 (and then I use HashMap and TreeMap where appropriately) and another class where I host them as List (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...

0

精彩评论

暂无评论...
验证码 换一张
取 消