A trade-off between guessing ArrayList's ability wrongly versus unused values?

Let's say I need to read data that can be either 1 object (most of the time) or multiple objects (sometimes).

If I do this:

List list = new ArrayList<Object>(1);
... loop over the loaded object(s) and add it/them to the list...

      

This will be useful to me in most cases when only one object is loaded from the database. But assuming a less common scenario, when I need to expand my original list, it will result in lost operations.

I'm guessing it won't have much effect on the real world, but I'm wondering how I could calculate the following:

Suppose X% of my data is 1 object and Y% is a list of multiple objects. Is there a way that I can calculate the ideal starting capacity for my list, for the smallest operations (via list extensions, highlighted but unused fields in the list)?

+3


source to share


1 answer


You are separating your data from 2 groups X (1 item) and Y (more than one). You've optimized your code for group X because this is the most common case.

It's a good idea to initialize the ArrayList with a single element, so you don't waste any memory most of the time.

But if the members of group Y have an average average size (and a small standard deviation), you can still optimize the worst case by providing capacity (int cap). In the second iteration, you can force the size of the ArrayList array to the average size of the Y group.

For a member of group Y with 100 elements, it will create / copy arrays 12 times, and the length of the underlying array will be 141 versus 1 small copy of the array, and there will be no wasted memory if you implement the optimization.



An example of this optimization:

Iterator<Obj> it = // Get your iterator from your resource
ArrayList<Obj> result = new ArrayList<Obj>(1);
if(it.hasNext()) {
    result.add(it.next());
}
if(it.hasNext()) {
    result.ensureCapacity(100);// Avg size of the Y group
    while(it.hasNext()) {
        result.add(it.next());
    }
}

      

But if it's not a critical performance metric, it's not worth the effort. To make sure this trick optimizes speed and memory, you need to analyze the size distribution in group Y.

This is incorrectly related to your problem, but contains many helpful comments on ArrayList: When to use LinkedList over ArrayList?

+2


source







All Articles