ArrayList ensureCapacity() method in Java
In this tutorial, we will see the ensureCapacity(int minCapacity)
method of the ArrayList class in Java.
Introduction
ArrayList is a widely used class to store and retrieve data in a collection framework.
Unlike an array, ArrayList can dynamically grow in size.
In previous articles, we have various methods to perform operations on the ArrayList.
A few methods of ArrayList class are add()
, addAll()
, remove()
, set()
, iterate()
, clear()
, contains()
, get()
, removeRange()
, retainAll()
, trimToSize()
, indexOf()
and lastIndexOf()
, removeIf()
and removeAll()
method.
Today we will see the ensureCapacity(int minCapacity)
method.
ArrayList ensureCapacity(int minCapacity)
method
Before understanding the ensureCapacity(int minCapacity)
method, we should know the basics of the ArrayList class in Java.
ArrayList internally uses a dynamic array so it can grow dynamically.
The Initial default capacity for the ArrayList is 10. And load factor is 0.75F. So, while adding the 7th element, the new ArrayList with double capacity is created, and the remaining elements of the array get added to a new array.
When you have to ensure that your ArrayList should hold an x
number of elements, you should invoke the ensureCapacity(int x)
method on the ArrayList.
So, This method increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
The method signature is as given below.
1
public void ensureCapacity(int minCapacity)
This method accepts only one parameter, i.e. minCapacity
of type int
minCapacity
- the desired minimum capacity
This method has a return type as a void
which means it will not return anything.
Let’s see a Java program where I will invoke the ensureCapacity(int minCapacity)
method on the ArrayList object.
In ArrayList, class we do not have any method which gives us the current capacity, so I will be not able to show the increased capacity on the console, but since we are invoking the ensureCapacity()
method, the capacity will be surely increased.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* An example Java program for ensureCapacity(int MinCapacity) method
* of the ArrayList class in Java.
* @author Gaurav at coderolls.com
*
*/
public class ArrayListEnsureCapacityExample {
public static void main(String[] args) {
// create an empty ArrayList object 'states'
ArrayList<String> states = new ArrayList<>();
//Add state in the ArrayList
states.add("California");
states.add("Texas");
states.add("Montana");
states.add("Arizona");
states.add("Florida");
states.add("Michigan");
states.add("New Jersey");
states.add("Washington");
states.add("Ohio");
states.add("Minnesota");
states.add("Colorado");
states.add("Missouri");
states.add("Nevada");
System.out.println("ArrayList Elements are: \n"+ states);
//We need to add more states, so we will ensure
//that ArrayList should hold 50 element
states.ensureCapacity(50);
System.out.println("\nWe ensured that the arraylist should hold 50 elements.");
}
}
Output:
1
2
3
4
ArrayList Elements are:
[California, Texas, Montana, Arizona, Florida, Michigan, New Jersey, Washington, Ohio, Minnesota, Colorado, Missouri, Nevada]
We ensured that the ArrayList should hold 50 elements.
Conclusion
1
public void ensureCapacity(int minCapacity)
This method increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
The example java program used in the above article can be found at this GitHub repository.
Please write your thoughts in the comment section below.
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!