ArrayList ensureCapacity() method in Java

The ensureCapacity() method is used to increase the capacity of an ArrayList to a specified value. In this post, we will look at the ensureCapacity() method of the ArrayList class in detail.

ArrayList internally uses an array to store the elements, and that array size is what we call the capacity of the ArrayList. So, whenever we increase the capacity of an ArrayList, it essentially means increasing the size of the underlying array.

  • Method declaration – public void ensureCapacity(int minCapacity)
  • What does it do? – It will increase the capacity of the ArrayList so that it can at least hold the number of elements specified by the minCapacity argument.
  • What does it return? – It will not return anything as its return type is void
Why would we need to use the ensureCapacity() method?

The default capacity of an empty ArrayList is 10. So, when the size of an ArrayList becomes equal to its capacity, then the memory reallocation happens, and the capacity of the ArrayList is increased to accommodate the new elements. It would happen every time the capacity becomes equal to ArrayList’s size, which would increase the amount of incremental reallocation and thus will impact the performance of the system.

To avoid this, we can use the ensureCapacity() method before adding a large number of elements to increase the capacity to a certain point all at once which would reduce the amount of incremental reallocation and can improve our system’s performance.

Code example
public class Codekru {

	public static void main(String[] args) {

		ArrayList<String> al = new ArrayList<String>();
		al.add("first");
		al.add("second");

		System.out.println(al); // printing the list

		System.out.println("capacity of the arraylist is 10 right now"); // default capacity of the arraylist is 10

		al.ensureCapacity(50); // increasing capacity to 50

		System.out.println("Capcity of the arraylist is increased to 50 now");

	}
}

Output –

[first, second]
capacity of the arraylist is 10 right now
Capcity of the arraylist is increased to 50 now

Discussions

Q- Can we decrease the capacity using the ensureCapacity() method?

No, we cannot decrease the capacity using the ensureCapacity() method.

Let’s say we started with an array list and began to add elements to it. Right now, the default capacity of the ArrayList would be 10, and if we use ensureCapacity with minCapacity=5, then the capacity of the ArrayList won’t change, and its value would still be 10.

public class Codekru {

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

		ArrayList<String> al = new ArrayList<String>();

		al.add("first");

		System.out.println("The capacity of the arraylist is 10");

		al.ensureCapacity(5);

		System.out.println("Here also, the capacity is still 10");

	}
}

Output –

The capacity of the arraylist is 10
Here also, the capacity is still 10
Q- Can we directly insert an element at the capacity’s last index?

No, we cannot do that. We have only increased the capacity of the ArrayList and not its size. Increasing capacity would only help in reducing the incremental reallocation. The below program might further clarify this point.

public class Codekru {

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

		ArrayList<String> al = new ArrayList<String>();

		al.add("first");

		al.ensureCapacity(100);

		// this will throw error as size of arraylist is still 1
		al.add(50, "fifty"); 

	}
}

Output –

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 50, Size: 1
	at java.base/java.util.ArrayList.rangeCheckForAdd(ArrayList.java:756)
	at java.base/java.util.ArrayList.add(ArrayList.java:481)

Here, the code has thrown an IndexOutOfBoundsException which shows we are trying to insert the element outside of its size boundaries.

Time complexity of the ArrayList ensureCapacity() method
  • The best case time complexity of ensureCapacity() method is O(1) when the capacity passed in the argument is less than the ArrayList original capacity.
  • The worst-case time complexity of ensureCapacity() method is O(n) when the underlying array’s size is increased. The new capacity of the ArrayList will be equal to the capacity passed in the function’s argument.

Referenced Article – https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#ensureCapacity-int-

Please visit this link to learn more about the ArrayList class of java and its other functions or methods.

We hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

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