views:

368

answers:

4

While creating classes in Java I often find myself creating instance-level collections that I know ahead of time will be very small - less than 10 items in the collection. But I don't know the number of items ahead of time so I typically opt for a dynamic collection (ArrayList, Vector, etc).

class Foo
{
  ArrayList<Bar> bars = new ArrayList<Bar>(10);
}

A part of me keeps nagging at me that it's wasteful to use complex dynamic collections for something this small in size. Is there a better way of implementing something like this? Or is this the norm?

Note, I'm not hit with any (noticeable) performance penalties or anything like that. This is just me wondering if there isn't a better way to do things.

+1  A: 

The overhead is very small. It is possible to write a hybrid array list that has fields for the first few items, and then falls back to using an array for longer list.

You can avoid the overhead of the list object entirely by using an array. To go even further hardcore, you can declare the field as Object, and avoid the array altogether for a single item.

If memory really is a problem, you might want to forget about using object instances at the low-level. Instead use a larger data structure at a larger level of granularity.

Tom Hawtin - tackline
Nothing that you're saying is wrong, per se, but I wouldn't want to do any of these things. The complexity involved in somehow dynamically going between object -> array -> ArrayList is so great, and the cost of just creating a simple ArrayList so small, that I think this is just a bad sugges
Outlaw Programmer
It's rarely important, but when it is important it is fixable. For instance, javac does weird stuff to keep memory used by strings small, so we all benefit from faster, smaller compiles.
Tom Hawtin - tackline
+9  A: 

The ArrayList class in Java has only two data members, a reference to an Object[] array and a size—which you need anyway if you don't use an ArrayList. So the only advantage to not using an ArrayList is saving one object allocation, which is unlikely ever to be a big deal.

If you're creating and disposing of many, many instances of your container class (and by extension your ArrayList instance) every second, you might have a slight problem with garbage collection churn—but that's something to worry about if it ever occurs. Garbage collection is typically the least of your worries.

John Calsbeek
Current JVMs optimize heavily for object allocation and garbage collection concerns wrt to this are almost always unfounded.
Boris Terzic
+3  A: 

For the sake of keeping things simple, I think this is pretty much a non-issue. Your implementation is flexible enough that if the requirements change in the future, you aren't forced into a refactoring. Also, adding more logic to your code for a hybrid solution just isn't worth it taking into account your small data set and the high-quality of Java's Collection API.

Kamikaze Mercenary
+2  A: 

http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/package-summary.html">Google Collections has collections optimized for immutable/small number of elements. See Lists.asList API as an example.

Cagatay