Home > Java, programming, regular > Apache Commons Primitive Collections

Apache Commons Primitive Collections

Do you ever work with libraries that require you to pass around primitive arrays?  If you do, you’ve probably run into the pain involved with trying to create these arrays if the underlying data is variably sized.  Why is it painful in Java?  Because the wonderful Collection classes that exist are for Objects only, and not primitive types.  There are no variable sized collections for primitive types.  So you might find yourself doing the following:

// need a double[] matching some criteria for a library call

List doublesMatching = new ArrayList();

// populate the list

double[] array = new double[doublesMatching.size()];

for (int i = 0; i < array.length; i++) {

  array[i] = doublesMatching.get(i);


// Use the array


Apache Commons has a whole slew of variable sized primitive collections, making your intent much clearer, and your code shorter.  Furthermore, if you have to deal with a huge amount of these primitive types, you gain a substantial space boost by not having the autoboxed object bloat.

Here is that same code above, avoiding all of the autoboxing and copying:

DoubleCollection doubles = new ArrayDoubleList();

// Populate doubles list; autoboxing is avoided


Categories: Java, programming, regular
  1. March 1, 2016 at 5:20 pm

    Das ist eine schmackhafte Pflanze, die zu vielen Gerichten passt, und ebenfalls beim
    Senken des Blutdrucks helfen kann.

  2. March 15, 2016 at 12:56 am

    Es scheint also deutlich sinnvoller zu sein, sich Hobbys zu suchen,
    bei denen man in Bewegung bleibt, als solche, die man sitzend
    erledigen kann.

  1. January 28, 2011 at 5:39 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: