How to Increase and Decrease Current Capacity (Size) of ArrayList in Java

By Saruque Ahamed Mollick

Java ArrayList is initialized by a size and the default size of ArrayList in Java is 10.

Java ArrayList is nothing but a part of Java Collection Framework and resided in Java.util package.



So in this post, we are going to know how can we change the size of ArrayList or in other words how to modify the size or length of an ArrayList.

How To Convert An ArrayList to Array In Java

 

To do so, there are two easy methods

  • ensureCapacity()
  • trimToSize()

Merge (Combine) Two or More Arrays Into A Single Array In Java

Using trimToSize() we can reduce the size or capacity of an ArrayList and using ensureCapacity() method we can increase the size of an ArrayList in Java.

 

Increase The Capacity Of ArrayList In Java

import java.util.ArrayList;

public class ArrayList {
     public static void main(String[] args) {
         
        ArrayList<Integer> numbers=new ArrayList<Integer>();
       // now the capacity of the ArrayList numbers is 10
       numbers.ensureCapacity(15);
           // now the capacity will be 15
       
      
    }
}

But here you guys might fall in a thought that if we check the size what will be the output:
For that, I will clarify it

import java.util.ArrayList;


public class ArrayList {
     public static void main(String[] args) {
         
        ArrayList<Integer> numbers=new ArrayList<Integer>();
       // now the capacity of the ArrayList numbers is 10
       System.out.println("size of numberes is "+numbers.size());
       numbers.ensureCapacity(15);
           // now the capacity will be 15
       
      
    }
}

 

 

run:
size of numberes is 0
BUILD SUCCESSFUL (total time: 0 seconds)

A Java Program To Find Duplicate Values In an Array

 

so we can see that the size will be shown as 0 (zero) but we have to keep in mind that it is because the list has no elements yet. It is actually showing the elements present in the list. But the default capacity is 10

numbers.ensureCapacity(15); this method will increase the capacity of the ArrayList.

Decrease The Capacity of ArrayList in Java

trimToSize() 

the method can be used to decrease the capacity of an ArrayList to the current list size. It is generally used to save up the memory, or we can say that it is used for memory optimization.
Let us assume that we have defined an ArrayList with size 15. But we have only 12 elements in that list, so in this case, if we apply trimToSize method then the capacity will be set as 12.

import java.util.ArrayList;

public class ArrayList {
     public static void main(String[] args) {
         
        ArrayList<Integer> numbers=new ArrayList<Integer>();
       // now the capacity of the ArrayList numbers is 10
       numbers.add(5);
       numbers.add(4);
       numbers.add(3);
       numbers.add(9);
       numbers.trimToSize();
           // now the capacity will be 4
       
      
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *