Memory management HashMap versus ArrayList.
Okay, here is my question. I have been trying to research on the memory hit an application is going to incur if it uses a HashMap instead of an ArrayList and I have not been able to find a satisfactory answer.
Suppose I have say 10,000 objects and the way I access them is such that 50% of time I end up iterating through the whole list, and the other 50% I have to access its entries randomly, then what should I opt for a HashMap or an ArrayList?
The way the data is stored in my HashMap is such that a String is used as a key. So according to me, at the very least not including HashMap抯 internal overhead I am going to waste memory just by creating a String object to represent the 慘ey?and I would be better of using an ArrayList in terms of memory utilization and will not experience a major performance hit.
Does anyone have a take on this?
# 1
If your keys are strings I don't see how you can use ArrayList at all. You can use HashSet, HashMap, TreeSet, or TreeMap. You can avoid the creation of extra key objects by using one of the Sets with a suitable Comparator.
ejpa at 2007-7-12 22:12:15 >

# 2
I don't think u understood my Q. Of course I am not going to use "key's" if I use an ArrayList!
My Q was about the amount of memory that will be saved if I use an ArrayList instead of a HashMap as now I no longer will need to allocate memory for the keys that are required in a HashMap implementation.
# 3
1) If you have a use for a map, the memory used by the key is not "wasted."
2) You won't necessarily be creating a new String object for the key. For instance, if the string you're using as a key is a field of the value object, then you'll just store a reference to it, not a whole new copy of the string.
3) Whether you should use a list or a map cannot be determined from the information given. However, if I know that I'll want to access an element by some key--evenif it's infrequently--I'd go with a map.
jverda at 2007-7-12 22:12:15 >

# 4
> If your keys are strings I don't see how you can use
> ArrayList at all. You can use HashSet, HashMap,
> TreeSet, or TreeMap. You can avoid the creation of
> extra key objects by using one of the Sets with a
> suitable Comparator.
Um, Sets don't have keys.
jverda at 2007-7-12 22:12:15 >
