4

What is the best practice for initializing an ArrayList in Java?

If I initialize a ArrayList using the new operator then the ArrayList will by default have memory allocated for 10 buckets. Which is a performance hit.

I don't know, maybe I am wrong, but it seems to me that I should create a ArrayList by mentioning the size, if I am sure about the size!

8 Answers 8

6

Which is a performance hit.

I wouldn't worry about the "performance hit". Object creation in Java is very fast. The performance difference is unlikely to be measurable by you.

By all means use a size if you know it. If you don't, there's nothing to be done about it anyway.

The kind of thinking that you're doing here is called "premature optimization". Donald Knuth says it's the root of all evil.

A better approach is to make your code work before you make it fast. Optimize with data in hand that tells you where your code is slow. Don't guess - you're likely to be wrong. You'll find that you rarely know where the bottlenecks are.

8
  • Yeah, I agree..right now We are working on performance issues..As everything is working fine as of now according to the requirement. So, we are just bothered about the performance. I understand that the object creation in Java is very fast, but still they require some time. If the number of such ArrayList is large, then the time is noticeable too..!! There are other ways to improve the optimization, but is this could be a way??
    – Pradeep
    Aug 27, 2010 at 9:37
  • As I know that I always have a single object in the ArrayList which I had initialized, then is it correct way of initializing the ArrayList with 10 buckets, out of which 9 will always remain empty.
    – Pradeep
    Aug 27, 2010 at 9:38
  • 2
    If you ALWAYS have a single object in a List, I'd question why you've designed a class that uses a List. Why not a reference to the single object? And if you tell me "I MIGHT have more than one...", then your ALWAYS statement is incorrect.
    – duffymo
    Aug 27, 2010 at 9:39
  • Too many "ifs". First you only have one object, then it might be large. Which one is it? Sounds like a lot of confused hypotheticals. Do you have profiling data that says this list is the downfall of your app? If not, you're wasting your time.
    – duffymo
    Aug 27, 2010 at 9:40
  • 1
    You're wasting your time and ours.
    – duffymo
    Aug 27, 2010 at 9:59
3

If you know how many elements you will add, initialize the ArrayList with correct number of objects. If you don't, don't worry about it. The performance difference is probably insignificant.

3

This is the best advice I can give you:

  • Don't worry about it. Yes, you have several options to create an ArrayList, but using the new, the default option provided by the library, isn't a BAD choice, otherwise it'd be stupid to make it the default choice for everyone without clarifying what's better.
  • If it turns out that this is a problem, you'll quickly discover it when you profile. That's the proper place to find problems, when you profile your application for performance/memory problems. When you first write the code, you don't worry about this stuff -- that's premature optimization -- you just worry about writing good, clean code, with good design.
  • If your design is good, you should be able to fix this problem in no time, with little impact to the rest of the system. Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces. You may even be able to switch to a LinkedList, or any other kind of List out there, if that turns out to be a better data structure. Design for this kinds of flexibility.
  • Finally, Effective Java 2nd Edition, Item 1: Consider static factory methods instead of constructors. You may even be able to combine this with Item 5: Avoid creating unnecessary objects, if in fact no new instances are actually needed (e.g. Integer.valueOf doesn't always create a new instance).

Related questions


On ArrayList micromanagement

Here are some specific tips if you need to micromanage an ArrayList:

  • You can use ArrayList(int initialCapacity) to set the initial capacity of a list. The list will automatically grow beyond this capacity if needed.
  • When you're about to populate/add to an ArrayList, and you know what the total number of elements will be, you can use ensureCapacity(int minCapacity) (or the constructor above directly) to reduce the number of intermediate growth. Each add will run in amortized constant time regardless of whether or not you do this (as guaranteed in the API), so this can only reduce the cost by a constant factor.
  • You can trimToSize() to minimize the storage usage.

This kind of micromanagement is generally unnecessary, but should you decide (justified by conclusive profiling results) that it's worth the hassle, you may choose to do so.

See also

1

If you already know the size of your ArrayList (approximately) you should use the constructor with capacity. But most of the time developers don't really know what will be in the List, so with a capacity of 10 it should be sufficient for most of the cases.

10 buckets is an approximation and isn't a performance hit unless you already know that your ArrayList contains tons of elements, in this case, the need to resize your array all the time will be the performance hit.

1

You don't need to tell initial size of ArrayList. You can always add/remove any element from it easily.

If this is a performance matter, please keep in mind following things :

  1. Initialization of ArrayList is very fast. Don't worry about it.
  2. Adding/removing element from ArrayList is also very fast. Don't worry about it.
  3. If you find your code runs too slow. The first to blame is your algorithm, no offense. Machine specs, OS and Language indeed participate too. But their participation is considered insignificant compared to your algorithm participation.
1

If you don't know the size of theArrayList, then you're probably better off using a LinkedList, since the LinkedList.add() operation is constant speed.

However as most people here have said you should not worry about speed before you do some kind of profiling.

You can use this old, but good (in my opinion) article for reference. http://chaoticjava.com/posts/linkedlist-vs-arraylist/

0

Since ArrayList is implemented by array underlying, we have to choose a initial size for the array.

0

If you really care you can call trimToSize() once you have constructed and populated the object. The javadoc states that the capacity will be at least as large as the list size. As previously stated, its unlikely you will find that the memory allocated to an ArrayList is a performance bottlekneck, and if it were, I would recommend you use an array instead.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.