Find capacity of an ArrayList in Java. Size Vs Capacity in Java List example

This tutorial covers that  what is  size and capacity of an ArrayList and Vector  in java and also covers that  how to find the size and capacity of an arraylist and vector using java code.  You can also find  the formula here that is used to calculate the capacity of an arraylist and vector.

What is  size?  The number of elements stored in the List .

What is  capacity ?  The current capacity of a List that means maximum number of elements it can hold currently. Initial capacity is decided by the integer number passed to Constructor of the ArrayList & Vector.

Vector uses the below formula to increase the capacity when it crosses the current capacity.

 int newCapacity = (capacityIncrement > 0) ? (oldCapacity + capacityIncrement) : (oldCapacity * 2)    ———>(A)

ArrayList uses the below formula to increase the capacity.

int newCapacity = (oldCapacity * 3)/2 + 1      ——–>(B)

Vector have methods size() & capacity() to return the current size & capacity where as ArrayList has size() method but it does not have capacity() method.  Now let us find capacity of an ArrayList. ArrayList uses an internal array buffer (Object elementData[]) to store the elements. The capacity of the ArrayList is the length of this array buffer which can be returned by length property of an array. As  elementData[] is a private array field in the ArrayList, how to access the private field and find the length?.  We can use the reflection feature of Java to find the length which is the capacity of the arraylist.

package net.javaonline;

import java.lang.reflect.Field;

import java.util.*;

public class CapacityArrayList {

     public static void main(String[] args) throws Exception  {

        ArrayList<Integer>  al = new ArrayList<Integer>(100);

       Vector<Integer>  vector = new Vector<Integer> (100); 

        for (int i = 0; i < 125; i++)

               {

	           al.add(i);

	           vector.add(i);

             }

       System.out.println("Array List  : Size =  "+ al.size() + " Capacity = "+  findCapacity(al));

       System.out.println("Vector  : Size =  "+ vector.size() + " Capacity = "+  vector.capacity());

    }

     //returns capacity of an ArrayList

  static int findCapacity(ArrayList<Integer> al) throws Exception {

        Field field = ArrayList.class.getDeclaredField("elementData");

        field.setAccessible(true);

        return ((Object[]) field.get(al)).length;

    }

}  
Output of the above program:

D:\Test>java CapacityArrayList

Array List : Size = 125 Capacity = 151

Vector : Size = 125 Capacity = 200

How the capacity is calculated ?

Initial Capacity of both AL and Vector is 100;

125 elements are added which crosses the initial capacity.

1. now the capacity of ArrayList is calculated as follows

oldcapacity = 100;

newCapacity = (100*3)/2 +1 = 151. refer the formula B

2. capacity of Vector is calculated as follows

capacityIncrement=0;

so newCapacity = (100*2) = 200. refer the formula A

You may also like

Leave a Reply

Be the First to Comment!