Java ArrayList ensureCapacity() Method with Example

In the previous article, we have discussed about Java ArrayList lastIndexOf() Method with Example

In this article we are going to see the use of Java ArrayList ensureCapacity() method along with suitable examples.

Java ArrayList ensureCapacity() Method with Example

ensureCapacity():

This java.util.ArrayList.ensureCapacity() method is used to set the size of the ArrayList. It means the arraylist can hold minimum that specified number of elements.

The important point is after ensuring arraylist cacapcity why it holds minimum that number of elements rather it should be maximum that number of elements. As you know ArrayList is resizable in nature hence the arraylist can be dynamically resized if more elements will be added than specified capacity.

Then the question comes to mind if we can add more elements than the specified capacity then what is the need of ensureCapacity() method. Actually to avoid multiple times of resize of the arraylist in case of huge number of add() operations are performed, in this case the arraysize will be resized at once only.

Syntax:

arrayListName.ensureCapacity(int minimumCapcity)

Where,

  • arrayListName refers to the name of your ArrayList.
  • minimumCapacity refers to minimum size of ArrayList.

Let’s see different examples to understand it more clearly.

Method-1: Java ArrayList ensureCapacity() Method – Example with String Type ArrayList

Approach:

  • Create a new ArrayList of type String.
  • Set the capacity of the arrayList using ensureCapasity() method
  • Add string elements into the ArrayList using the add() method.
  • Display the ArrayList elements

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a ArrayList of string datatype
        ArrayList<String> arr = new ArrayList<String>();
        // set the capacity of the arraylist
        arr.ensureCapacity(3);
        // Adding 3 string elements to the ArrayList as specified capacity
        arr.add("Bhubaneswar");
        arr.add("Hyderabad");
        arr.add("Kolkatta");
        
        //even we can add more elements also as it is dynamically resizable
        arr.add("Chennai");
        arr.add("Bhopal");
        
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
    }
}
Output:

The elements of ArrayList are: [Bhubaneswar, Hyderabad, Kolkatta, Chennai, Bhopal]

Method-2: Java ArrayList ensureCapacity() Method – Example with Integer Type ArrayList

Approach:

  • Create a new ArrayList of type Integer.
  • Set the capacity of the arrayList using ensureCapasity() method
  • Add integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create a ArrayList of string datatype
        ArrayList<Integer> arr = new ArrayList<Integer>();
        // set the capacity of the arraylist
        // arraylist can hold minimum 100 number of elements
        arr.ensureCapacity(100);
        // Adding some integer elements to the ArrayList
        arr.add(100);
        arr.add(101);
        arr.add(102);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
    }
}


Output:

The elements of ArrayList are: [100, 101, 102]

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: