Internal working of ArrayList in java

ArrayList in java is the most popular topic in java and also popular for widely use due to flexible size.  In java Array and ArrayList, both works based on indexes and both are famous for random access. But Array has fixed length, on another hand the length of ArrayList is flexible according to use. In this post, we will see how ArrayList increases or decrease the length and Internal working of ArrayList in java

You can read it with an example from here.
1. The Default size of ArrayList in java?
2. Internal working of ArrayList in java?
3. Best practices to create an ArrayList

The default size of ArrayList in java

When we create an ArrayList by default constructor of ArrayList class, then JVM creates an ArrayList in heap memory with the default size of ArrayList in java. The default size of ArrayList in java is 10. But an ArrayList is a growable array, unlike array it doesn’t have a fixed length. It increases the size dynamically whenever we add or remove any element in ArrayList.

ArrayList<Integer> list = new ArrayList<Integer>();
Internal working of ArrayList in java

We can initialize the capacity of ArrayList during the creation of ArrayList. The ArrayList class provides a constructor that takes capacity as an integer value. Suppose we want to create an ArrayList with capacity 15.

ArrayList<Integer> list = new ArrayList<Integer>(15);
Internal working of ArrayList in java

But it can take more than 15 elements because when we add element it will automatically increase itself. Let’s see it with example.

import java.util.ArrayList;
public class ArrayListExample
   public static void main(String args[])
	   ArrayList<Integer> list = new ArrayList<Integer>();
	   for(int i = 0; i < 20; i++)
	   System.out.println("Elements from ArrayList: "+ list);

Output: Elements from ArrayList: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Internal working of ArrayList in java?

The size of ArrayList grows automatically which is fully based on load factor and current capacity. Basically, the load factor is the measure that decides when to increase the capacity of the ArrayList. The default load factor of an ArrayList is 0.75f.

ArrayList increases the capacity after each threshold. Now the question arises how the threshold is calculated?

Threshold = (Current Capacity) * (Load Factor)

For example, if the ArrayList has 10 element and load factor of 0.75f, then threshold will be,

Threshold = 10 * 0.75 = 7

Let’s take an example and understand how ArrayList grows in memory. Suppose we have an ArrayList with default capacity 10. We can add more than 10 elements in ArrayList.
Whenever we perform add operation, it checks whether the threshold value is equal to the number of elements present in ArrayList. If the size of the current element is greater than the threshold. The JVM creates a new ArrayList and copies the old ArrayList into new. It creates the ArrayList size 10 to 20.

Internal working of ArrayList in java

Best practices to create an ArrayList

Whenever ArrayList reaches its threshold, it creates a new ArrayList object with new capacity and copies all old elements from old ArrayList to a new object. This process is space and time consuming even it provides flexibility. So, It would be good practice if we choose the initial capacity, by keeping the number of expected elements in mind.

We should take care of while choosing the load factor. The ArrayList has a default load factor of 0.75f always gives the best performance in terms of both space and time.

Leave a Comment