I'm working on a 2D game for Android using LibGDX in Java. In my game I got lots of enemies, bullets and other stuff. I hold a hash map, which every cell holds a reference to an array list of that kind of objects. For example:

HASH MAP->

Enemies = [enemy1,enemy2,enemy3]

Bullets = [bullet1,bullet2] etc...

In my render() loop I run with an iterator on the hash map and run through all the objects in every array list, calling each one's update() method.

I wonder, would it be faster holding a linked list to every kind of objects instead of array list and running through that linked list?

Thanks!

• So you're asking whether LinkedList is faster than ArrayList, and your question has little to do with HashMap? – immibis Feb 2 '16 at 23:16
• Well, hash map can also be replaced with a linked list while every link has a reference to a linked list so the question can be also related to that. But basically, yes. – Gad Wissberg Feb 2 '16 at 23:19
• This sort of relates to premature optimization, the advice I would give is unless you're aware that your current solution isn't performing well enough it's not worth messing with. On top of that, more than likely you will not be able to noticeably improve your current solution. You have a fixed cost of iterating through the total number of enemies which almost certainly dominates your computation time compared to the HashMap iteration. – Alex Sherman Feb 2 '16 at 23:34

If all you are doing is iteration, stick with the ArrayList. The difference in iteration performance for an ArrayList versus a linked list is small-to-nonexistent, and favors the ArrayList (because of contiguity and cache misses).