Java Vector ensureCapacity() Method
ensureCapacity() is a method in Java's Vector A method provided by the class, used to ensure that the Vector can hold the specified number of elements without reallocating internal array space. This method can help us optimize the Vector 's performance, especially when it is known in advance that a large number of elements need to be stored.
Method Syntax
public void ensureCapacity(int minCapacity)
Parameter Description
minCapacity: The minimum capacity value to ensure
Return Value
The method has no return value (void type)
Method Effect
ensureCapacity() The main effects of the method are:
- If the current
Vector's capacity is less than the specified minimum capacity, increase its capacity - If the current capacity is already large enough, do nothing
- By pre-allocating enough space, the number of expansions when adding elements later can be reduced
Usage examples
Basic Usage
Example
import
java.util.Vector
;
public
class
VectorExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a Vector with an initial capacity of 5
Vector
<
String
vector
=
new
Vector
<>
(
5
);
System
.
out
.
println
(
"Initial capacity: "
vector.
capacity
(
)
)
;
// Ensure the capacity is at least 10
vector.
ensureCapacity
(
10
)
;
System
.
out
.
println
(
"Capacity after calling ensureCapacity(10): "
vector.
capacity
(
)
)
;
}
}
Output result
初始容量: 5
调用ensureCapacity(10)后的容量: 10
Practical Application Scenarios
Example
import
java.util.Vector
;
public
class
BulkDataProcessing
{
public
static
void
main
(
String
[
]
args
)
{
// Suppose we know we need to process about 1000 records
Vector
<
Integer
data
=
new
Vector
<>
(
);
// Pre-allocate enough space
data.
ensureCapacity
(
1000
)
;
// Add a large amount of data
for
(
int
i
=
0
;
i
<
1000
;
i
++
)
{
data.
add
(
i
)
;
}
System
.
out
.
println
(
"Final capacity: "
data.
capacity
(
)
)
;
System
.
out
.
println
(
"Number of elements: "
data.
size
(
)
)
;
}
}
Output result
最终容量: 1000
元素数量: 1000
How It Works
Vector internally uses an array to store elements. When adding a new element, if the current array is full, theVector automatically creates a larger array and copies all elements into the new array. This process is called "expansion".
ensureCapacity() - If a security manager exists and denies access to the file
- Checks whether the current capacity is less than the minimum capacity specified by the parameter
- If so, creates a new, larger array
- Copies the existing elements into the new array
- Updates the internal reference to point to the new array
Performance considerations
Why Use ensureCapacity()
- Reduce the Number of Expansions: Frequent expansion affects performance, especially when processing large amounts of data
- Memory Optimization: Pre-allocating enough space avoids multiple small expansions
- Performance Improvement: Reduces array copy operations and improves overall efficiency
Expansion Strategy
If you do not use ensureCapacity(), Vector has its own expansion strategy:
- By default,
Vector's initial capacity is 10 - When expansion is needed, the capacity doubles by default (i.e., new capacity = old capacity * 2)
- A different growth amount can also be specified
Notes
ensureCapacity()Only increases capacity, never decreases it- If the specified minimum capacity is less than the current capacity, the method does nothing
- This method does not change the
Vector's size(); it only affects capacity() - Over-allocating capacity may waste memory space
Comparison with ArrayList
Although ArrayList also has ensureCapacity() method, but the two have some differences in implementation:
| Feature | Vector | ArrayList |
|---|---|---|
| Thread Safety | Yes (synchronized method) | No |
| Default Expansion Strategy | Capacity doubles | Capacity increases by 50% |
| Performance | Slightly slower (because of synchronization) | is usually faster than |
Summary
Vector 's ensureCapacity() The method is a useful tool that can help us optimize collection performance, especially when processing large amounts of data. By pre-allocating enough space, frequent expansion operations can be avoided, thereby improving the overall efficiency of the program.
Remember, when using this method, set the minimum capacity reasonably according to actual needs—neither too small (causing frequent expansion) nor too large (causing memory waste).
Java Vector