<p>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).</p>

<pre><code>class Foo
{
  ArrayList&lt;Bar&gt; bars = new ArrayList&lt;Bar&gt;(10);
}
</code></pre>

<p>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?</p>

<p>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.</p>

[{'answer_id': 57177, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 57185, 'author': 'John Calsbeek', 'author_id': 5696, 'author_profile': 'https://Stackoverflow.com/users/5696', 'pm_score': 4, 'selected': True, 'text': "<p>The <code>ArrayList</code> class in Java has only two data members, a reference to an <code>Object[]</code> array and a size—which you need anyway if you don't use an <code>ArrayList</code>. So the only advantage to not using an <code>ArrayList</code> is saving one object allocation, which is unlikely ever to be a big deal.</p>\n\n<p>If you're creating and disposing of many, many instances of your container class (and by extension your <code>ArrayList</code> instance) every second, you <em>might</em> 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.</p>\n"}, {'answer_id': 57226, 'author': 'Aaron', 'author_id': 2628, 'author_profile': 'https://Stackoverflow.com/users/2628', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 57341, 'author': 'Cagatay', 'author_id': 3071, 'author_profile': 'https://Stackoverflow.com/users/3071', 'pm_score': 2, 'selected': False, 'text': '<p><a href="https://web.archive.org/web/20081018003459/http://google-collections.googlecode.com:80/svn/trunk/javadoc/index.html?http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/package-summary.html" rel="nofollow noreferrer">Google Collections</a> has collections optimized for immutable/small number of elements. See <code>Lists.asList</code> API as an example.</p>\n'}]
['https://Stackoverflow.com/questions/57145', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2881/']