1

In order convert a List to an array, you need an empty array like following:

List<Foo> fooList;
Foo[] foos = fooList.toArray(new Foo[0]);

I happen to use this code snippet a lot, so instead of creating an empty array every time, I decided to create a static empty array, and use it each time. e.g.

private static Foo[] EMPTY_FOOS = new Foo[0];

List<Foo> fooList;
Foo[] foos = fooList.toArray(EMPTY_FOOS);

Is this worth it or are we looking at a difference of 0.000000000000000000000000000000000000000000000000000001ms?

13
  • It can be worth it. This is what profiling is for. Only you can determine what is a bottleneck in your own code. Commented Feb 24, 2012 at 23:01
  • Bit optimization is root of problems. Commented Feb 24, 2012 at 23:03
  • 2
    @Saeed, nonsense. The problem is optimizations before any observation of an actual problem. Commented Feb 24, 2012 at 23:05
  • Is there a specific reason Why you don't use the parameter less toArray() function? Commented Feb 24, 2012 at 23:05
  • 2
    @KirkWoll, If another developer use this static field it can be a devil. Commented Feb 24, 2012 at 23:09

3 Answers 3

7

Actually I believe this is the best performing version:

fooList.toArray(new Foo[fooList.size()]);

This way toArray() always has the exact array and no extra arrays are created. IntelliJ suggests such a refactoring out of the box.

Sign up to request clarification or add additional context in comments.

3 Comments

Excellent point, +1. If you don't do this, it will allocate more arrays "under the hood" anyways.
Or just don't use Java's "array" objects at all directly? (ArrayList, etc, use them internally.) They are really outcasts in the Java Collection framework.
If you pass an array of anything less than the size of the collection (including 0)., it will create exactly one more array (not "arrays") of exactly the correct number of elements. However, it does use reflection to do that, so your way is still preferable.
2

On my computer, creating 100,000,000 Object[0] arrays takes 2.9 seconds. If you are creating a million arrays per second, or even 100,000, it might make a measurable difference.

Comments

1

I think it would make more sense to question why you need to convert arrays to Lists and vice versa in the first place. You'll be better off sticking with the Collection classes if you can and avoiding the use of arrays as they are more error prone that using Collection classes.

If you use that snippet of code 'a lot' then write a method that you can reuse, but in order to determine what effort you should invest in optimizing this code, you need to profile it and see what the overhead is in relation to your other code and how often this code is actually used at runtime.

As Knuth said: "premature optimization is the root of all evil" :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.