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.

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.



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

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;


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.



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.


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.





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.


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


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.


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.



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


No comments:

Post a Comment