Importance of specifying initial capacity while instantiating ArrayList and HashMap

ArrayList and HashMap are a few of the most frequently used data structures in Java development. So I would like to talk about the importance of instantiating these classes with initial capacity. Let me explain how the ArrayList works internally, so that you get a good insight. HashMap works almost the same way an ArrayList works, so I haven’t covered the details of that separately. But all the tips given below are applicable for HashMap as well.

As the name implies, ArrayList is a list of array elements (i.e. Object[]). ArrayList has some good utility methods to increment array size and manipulate arrays easily.

Let us take the following example:

ArrayList sampleList = new ArrayList();

When the JVM executes this statement, it creates an array with size of 10 (default capacity) in the memory. As it creates just 10 elements, it doesn’t mean that you can add only 10 elements in this list; you can add more than 10 elements. When you add an11th element to this list, ArrayList internally extends its array size. It creates a new array with size of 20, and copies the old 10 elements to the new array and adds the 11th element to the array. So the old array with size 10 will be sending for garbage collection.

For this if we use the following code, the arraylist will create 11 arrays initially, so that when you add the 11th element, the array size need not be extended.

ArrayList sampleList = new ArrayList(11);

The advantages are:

  1. We can improve the memory usage
  2. We can improve the performance (i.e. additional time for creation of arrays can be saved)

Some of the tips that could be implemented in your project:

  1. Specify the initial capacity while instantiating ArrayList and HashMap
  2. If you don’t know the exact initial capacity, please perform an evaluation and come up with some approximate number.
  3. If you cannot even come-up with approx. initial capacity, then provide enough comments in the code that states the reason why initial capacity could not be provided in that case.

10 responses to “Importance of specifying initial capacity while instantiating ArrayList and HashMap”

  1. hi,
    good post

  2. A comment received from my friend:

    One small correction in this example that is creating an object of ArrayList to an ArrayList reference. It would be more appropriate if it is a List or Collection reference (create object to super class or interface reference when ever possible).

    Secondly, ArrayList will increase the capacity to 50% where Vector will double the size. I mean second time it will increase the size by 16 (50%) and not 20 which is double.

    Thanks Sajeev and Hari.

  3. Really Very Good Post…………

  4. This is really good Post.
    Arraylist will increase its size by using following:
    int newCapacity = (oldCapacity * 3)/2 + 1; // extract from ArrayList class

  5. Thanks Kiran for your comments.

  6. Any thoughts on how conservative/liberal to be when estimating the size of the collection? Is it better to waste memory by allocating too much space or to waste resources re-creating the area?

  7. I would say be more liberal when specifying the initial capacity for temporary object. For long living objects you can always call trimToSize() method to minimize the object storage.

  8. really helpful example

  9. gtracing2hackz.wordpress.com

    I feel this is among the such a lot significant information for
    me. And i am glad reading your article. However wanna remark on few normal things,
    The web site style is perfect, the articles is truly great :
    D. Good process, cheers

  10. Awesome! Its actually amazing paragraph, I have got mmuch clear idea regarding from
    this paragraph.

Leave a comment

Blog at WordPress.com.