what is the default initial capacity of ARRAYLIST, how it is resized and size is increased in java


In this Collection framework tutorial we will learn what is the default initial capacity of ARRAYLIST, how it is resized and size is increased in java.


Contents of page >
  • 1) What is meaning of capacity in ArrayList in java?
  • 2) Does size of ArrayList grows automatically in java?
  • 3) What is default initial capacity of ArrayList in java?
  • 4) By what size ArrayList is resized in java? How much size increases when ArrayList is resized in java?
  • 5) But how ArrayList is resized in java?
  • 6) Let’s see java Example/program to see what is default initial capacity of ArrayList in java and how it is resized in java by putting java application debug mode?
  • 7) Can we change default initial capacity of ArrayList in java?
  • 8) Can we change resizing of ArrayList in java? The amount by which the capacity of ArrayList is increased when the ArrayList overflows?
  • 9) Should you change default initial capacity of ArrayList in java?
  • 10) How ArrayList is implemented in java?
  • 11) One more important concept related to ArrayList size, a MUST READ discussion on java.util.ArrayList internal methods >

1) What is meaning of capacity in ArrayList in java?
Capacity is the size of the array which is used to store elements in the ArrayList.

2) Does size of ArrayList grows automatically in java?
Yes, size of ArrayList grows automatically in java. ArrayList resizes itself dynamically in java.

3) What is default initial capacity of ArrayList in java?
Default initial capacity of ArrayList is 10.

java.util.ArrayList defines private static final variable DEFAULT_CAPACITY to define initial capacity of ArrayList.

   /**
    * Default initial capacity.
    */
private static final int DEFAULT_CAPACITY = 10;

4) By what size ArrayList is resized in java? How much size increases when ArrayList is resized in java?
ArrayList is resized by 50% of it’s current size.
So, ArrayList will be resized from 10, to 15, to 22, to 33 and so on.


5) But how ArrayList is resized in java?
ArrayList’s add method internally calls ensureCapacityInternal method, which calls ensureExplicitCapacity method, which calls grow method, grow method >
  • creates new array of higher capacity and
  • copies existing array to new one and
  • return the new array.

6) Let’s see java Example/program to see what is default initial capacity of ArrayList in java and how it is resized in java by putting java application debug mode?
import java.util.ArrayList;
import java.util.List;
public class ArrayListDefaultCapacityAndResizingExample {
public static void main(String args[]) {
     List<Integer> arrayList = new ArrayList<Integer>();
     for (int i = 1; i < 25; i++) {
          arrayList.add(i);
     }
}
}

When, new ArrayList<Integer>() is executed, Size of ArrayList is 0.


As soon as first element is added, using add(i), where i=1, ArrayList is initialized to it’s default capacity of 10.


Till addition of 10th element size of ArrayList remains same.


As soon as 11th element is added, using add(i), where i=11, ArrayList is resized to 15.


Till addition of 15th element size of ArrayList remains same.


As soon as 16th element is added, using add(i), where i=16, ArrayList is resized to 22.


Till addition of 22th element size of ArrayList remains same.


As soon as 23rd element is added, using add(i), where i=23, ArrayList is resized to 33.




7) Can we change default initial capacity of ArrayList in java?
Yes, rather than using new ArrayList(), you can use other constructor specified in java.util.ArrayList
   public ArrayList(int initialCapacity) {
       super();
       if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
       this.elementData = new Object[initialCapacity];
   }
This constructor will throw IllegalArgumentException if initialCapacity passed is less than 0.

8) Can we change resizing of ArrayList in java? The amount by which the capacity of ArrayList is increased when the ArrayList overflows?
No.

9) Should you change default initial capacity of ArrayList in java?
Well that is opinion based questions, but default size offers best tradeoff between memory occupied and performance.

I’ll recommend you to go for default initial capacity offered by ArrayList in java.

Keeping ArrayList size very less can be a huge performance set back, because it will be resized very rapidly.
Example - when it’s initial capacity is kept as 2, on addition of further elements it will be resized to 3,  then 4, then 6, then 9, then 13, then 19 and so on.
So we saw that resizing of ArrayList was done so rapidly and it may significantly slow down your java application.

But, huge enterprise application which is likely to store high number of objects may be benefited by increasing the default initial capacity offered by  ArrayList in java.

10) How ArrayList is implemented in java?
You must read, ArrayList custom implementation to get better understanding of ArrayList is formed using Array in java.

In this Collection framework tutorial we learned what is the default initial capacity of ARRAYLIST, how it is resized and size is increased in java.

11) One more important concept related to ArrayList size, a MUST READ discussion on java.util.ArrayList internal methods >

When, new ArrayList<Integer>() is executed, Size of ArrayList is 0.
Internally, When you call new ArrayList<Integer>() the constructor of ArrayList is called>
   public ArrayList() {
       super();
       this.elementData = EMPTY_ELEMENTDATA;
   }
Here we can see that initial size is EMPTY_ELEMENTDATA (its value is {} - i.e. 0 elements).

As soon as first element is added, using add(i), where i=1, ArrayList is initialized to it’s default capacity of 10.
   public boolean add(E e) {
       ensureCapacityInternal(size + 1);  // Increments modCount!!
       elementData[size++] = e;
       return true;
   }
   private void ensureCapacityInternal(int minCapacity) {
       if (elementData == EMPTY_ELEMENTDATA) {
        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
       }
       ensureExplicitCapacity(minCapacity);
   }
When add method is called, it internally calls ensureCapacityInternal method,
which further checks if elementData is equal to EMPTY_ELEMENTDATA (i.e. 0), then it assigns it value of DEFAULT_CAPACITY (using Math.max method initially the value is  10.)

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

RELATED LINKS>

List hierarchy in java - Detailed - ArrayList, LinkedList, vector, CopyOnWriteArrayList classes

ArrayList vs LinkedList - Similarity and Differences


ArrayList vs Vector - Similarity and Differences

ArrayList vs CopyOnWriteArrayList - Similarity and Differences with program


Consequence of using ArrayList in multithreading environment in java


ArrayList Programs >
ArrayList - add, add element at specific index methods program
ArrayList - remove, get, contains and set methods program
ArrayList - iterate using iterator, listIterator, Enumeration and enhanced for loop
ArrayList - fail-safe or fail-fast iteration using iterator, listIterator, Enumeration and enhanced for loop


eEdit
Must read for you :