The ensureCapacity() method of the ArrayList class is used to increase the internal capacity of an ArrayList so that it can accommodate a specified minimum number of elements. Preallocating capacity helps improve performance by reducing the number of internal array reallocations and copying operations when adding a large number of elements.
- Does not change the current size of the ArrayList.
- Does not remove or modify existing elements.
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
// create an ArrayList of Integers
ArrayList<Integer> l = new ArrayList<>();
// preallocate capacity for 5 elements
l.ensureCapacity(5);
// Add elements to the ArrayList
for (int i = 1; i <= 5; i++) {
l.add(i);
}
System.out.println(l);
}
}
Output
[1, 2, 3, 4, 5]
Explanation: In the above example, we preallocate capacity for an ArrayList of integers before adding 5 elements to optimize memory usage.
Syntax
public void ensureCapacity(int minCapacity)
- Parameter: minCapacity: The minimum desired capacity for the ArrayList.
- Return Value: The ensureCapacity() method does not return any value (void).
Example: Using the ensureCapacity() method to preallocate space for a String ArrayList and to add elements.
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
// Create an ArrayList of Strings
ArrayList<String> n = new ArrayList<>();
// Preallocate capacity for 7 elements
n.ensureCapacity(7);
// Add elements to the ArrayList
n.add("Ram");
n.add("Shyam");
n.add("Hari");
System.out.println(n);
}
}
Output
[Ram, Shyam, Hari]
Explanation: In the above example, we preallocate capacity for an ArrayList of strings and then add a few names to the list.
Example: Program to demonstrate the use of ensureCapacity() with a large dataset
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
// Create an ArrayList of Integers
ArrayList<Integer> l = new ArrayList<>();
// Preallocate capacity for 200 elements
l.ensureCapacity(200);
// Add 200 elements to the ArrayList
for (int i = 1; i <= 200; i++) {
l.add(i);
}
System.out.println("Size of an ArrayList: " + l.size());
}
}
Output
Size of an ArrayList: 200
Explanation: In the above example, we preallocate the capacity for an ArrayList to handle a large dataset of 200 elements.
Difference Between ensureCapacity() and trimToSize()
ensureCapacity() | trimToSize() |
|---|---|
| Increases the internal capacity if required. | Reduces the internal capacity to match the current size. |
| Used before adding many elements. | Used after removing elements to free unused memory. |
| Helps improve insertion performance. | Helps optimize memory usage. |
| Does not change the size of the list. | Does not change the size of the list. |
Advantages of ensureCapacity() Method
- Improves performance by reducing resizing operations.
- Minimizes internal array copying.
- Efficient for handling large datasets.
- Helps optimize memory allocation when the required size is known.
- Reduces the overhead of repeatedly expanding the ArrayList.