ArrayList, Hashtable, or ... for holding in-game-objects

This is my Bag implementation, which just extends ArrayList and overrides the remove method. Note that this class only sets indices to null, this is to avoid getting a ConcurrentModificationException while looping through with a for-each loop. After your foreach loop ends, just call remove(null) and all the nulls will removed from the list.

You’re using recursion to remove nulls from the collection. Calling indexOf(null) for every null element in your list adds insult to injury as it will remove the first null element from the list, resulting in moving a lot of (null) elements around in the backing array. Not only is that significantly slower than using iteration, but what if you just removed >10K of items, you’d end up with a StackOverflowError.

The bigger question here: why exactly are you removing >10K items?! :stuck_out_tongue:
And how much slower is recursion than iteration?

I updated the previous post. Seeing as how the Bag collection is purely meant for performance, you most likely made it perform only slightly faster than your regular ArrayList.

This is my (outdated) Bag class:

That’s not the bigger question at all, it’s not even relevant. You created a collection that is known to fail under normal conditions and has disappointing performance.

Maybe on disk but in memory… I can only see disadvantages. ArrayList ftw!

Cas :slight_smile:

When I put my tin foil hat on, I like to imagine the superiour performance of LinkedList<ArrayList> in some odd usecase.

Really, I still use ArrayList only :stuck_out_tongue:

Size is known: Arrays
Size is unknown: ArrayLists
Size is huge --> Probably vertex or other GPU data, so obviously just keep it in a ByteBuffer/FloatBuffer or MappedObjects

Well, that’s how I do it at the moment. xD

Wow, this is the BEST thread about lists I’ve ever read! :smiley:
So many good ideas here!

My 2 cents: I tend to use ArrayList. Their ‘sense’ of order appeases me.

I’d like to add, for the OP: Whichever you use (Bag, ArrayList, etc.), you probably wont see a noticable difference, until you’re working with the big boys on some voluminous project. But I must admit I share your enthusiasm for optimizing :wink:

Yes, that’s true :o)

Hehe. Me too. I use ArrayList and when I’m done, I profile to see what my bottlenecks are. I’ve never had a container class be the bottle neck so I’ve never ended up optimising it.

But… I still love to read about people’s optimising efforts. It satisfies the inner geek :o)

The main idea behind this problem is how big your object’s size.

Actually I think of main memory as being a drive.

You shouldn’t. Disk access is many orders of magnitude slower than memory. That’s why Cas’s comment about Lists not being ideal for n-memory storage needs even though it’s perfect for indexing into databases is well made.

Disk drive are like semaphores…not concurrency kind…the communication by flags, where the reader is blind.