Performance: HashMaps vs Arrays

Jonah11
Jonah11 used Ask the Experts™
on
I am writing a simulation program where speed is crucial.  From an OO design perspective, it makes most sense for me to use HashMaps whose keys are enum values in my model objects.  

I will be doing millions of operations on these HashMaps, looping over them many times, and doing lookups on them, including retrieving lists of keys whose value is equal to some specified value.

My question is this: Should I go ahead and use HashMaps, or should I consider using arrays, or some other structure, because they will be faster for these looping and lookup operations?

Thanks for any ideas.  I can give further details on what I'm doing if necessary.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Java Developer
Top Expert 2010
Commented:
Map will certainly be faster for the lookup as you would need to loop thru the array to lookup using an array.
For looping thru all elements then an array would be faster but probably not a lot in it (you'd need to do some benchmarking)

Though theres no reason you couldn't maintain an array and a Map, and use the array for looping, and the map for lookups

Top Expert 2016
Commented:
You won't get faster than a Map for a lookup operation. As far as iteration is concerned, HashMap is partially implemented using an array anyway. As far as

>>retrieving lists of keys whose value is equal to some specified value.

is concerned, you can reverse the Map.

Commenting on a specific skeleton implementation or interface would enable you to get more specific answers though



If your hash function is a good one that means all hash keys have roughly equal number of values
Then hash map become quicker in searching.
Best thing is to test it your own rather than rely on others answers that is based on 100% theory.
because there can be cases this become wrong
write ur own test program and test it and see
Good Luck!!!
If you are able to determine the exact number of the elements in advance, then creating of HashTable with this initial capacity will provide the same looping time as array will. Assuming that the default hashCode implementation will disperse the elements in the best way to the buckets you will have the best performance for this operation "retrieving lists of keys whose value is equal to some specified value" as well. For lookup opertations, as CEHJ stated "You won't get faster than a Map".
But if you have to insert and remove elements frequently and your elements are not so many, probably it will slow down the process.

Commented:
If your keys are enum values, consider using java.util.EnumMap. If will be certantly more efficient then generic HashMap.

Author

Commented:
Thanks for the info!
Top Expert 2016

Commented:
You might like to take a look at the following - you can 'look inside' the HashMap:

http://technojeeves.com/joomla/index.php/free/105-hashmap-log-debugged

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial