views:

167

answers:

3

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?

+1  A: 

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
Péter Török
Why couldn't you create a List of long?
Snake
@Snake, see my update.
Péter Török
I can't do anything you said because this is not my choice, it's what's required of me for this university project. I need these (and exactly the ones I mentioned) different data structures because the idea is to measure the performance of each one of them. I could easily create a different class for each one but I wanted to avoid that if possible.
Nazgulled
And what I meant by "duplicating" was having the user profile class with 4 private variables (one for each data structure) I only want to have one (or 2 at most, for `Map` and `List`) and work from there.
Nazgulled
@Nazgulled, hmmm... You need to measure the performance of what exactly? Different collection implementations? Why do you need the whole user profile stuff then (as opposed to just putting, accessing, removing etc. 1M items into different collections and measuring execution time)? I am afraid I still don't get the full picture...
Péter Török
You don't need to know the full picture and your questioning things for which I don't have an answer. I'm doing what I'm requested to do... The idea is not to measure the collections themselves, but the collections with a user profile. That's what I have to do.
Nazgulled
@Nazgulled, fair enough, if you want an answer based on incomplete facts, you get it ;-) I would go with the subclass approach then.
Péter Török
A: 

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.

DJClayworth
I need to have a class for the user profile and the messages need to be inside that class. And what I meant by "duplicating" was having the user profile class with 4 private variables (one for each data structure) I only want to have one (or 2 at most, for Map and List) and work from there.
Nazgulled
A: 

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

Nazgulled