Java Vector capacity() Method
capacity() The method is in Java's Vector A built-in method provided by the class, used to return the current capacity of the vector. Capacity refers to the number of elements the vector can store without reallocating its internal storage.
Syntax format
public int capacity()
Method Return Value
The method returns a int type value, representing the current Vector object's capacity.
Basic Usage
Example Code
Example
import
java.util.Vector
;
public
class
VectorCapacityExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a Vector with an initial capacity of 10
Vector
<
String
fruits
=
new
Vector
<>
(
10
);
// Add some elements
fruits.
add
(
"Apple"
)
;
fruits.
add
(
"Banana"
)
;
fruits.
add
(
"Orange"
)
;
// Get the current capacity
int
currentCapacity
=
fruits.
capacity
(
)
;
System
.
out
.
println
(
"Current Vector capacity: "
currentCapacity
)
;
}
}
Output result
当前Vector容量: 10
In-Depth Understanding
Vector Capacity vs Size
- Capacity: The size of the internal array of the Vector, i.e., the total number of elements it can store
- Size: The number of elements actually stored in the Vector at present
Example
Vector
<
Integer
numbers
=
new
Vector
<>
(
5
);
// Initial capacity is 5
numbers.
add
(
1
)
;
numbers.
add
(
2
)
;
System
.
out
.
println
(
"Capacity: "
numbers.
capacity
(
)
)
;
// Output 5
System
.
out
.
println
(
"Size: "
numbers.
size
(
)
)
;
// Output 2
Automatic Capacity Growth Mechanism
When the size of the Vector exceeds the current capacity, the Vector automatically increases its capacity. By default, the capacity doubles, but this behavior can be changed by specifying a capacity increment.
Example
// Create a Vector with an initial capacity of 3 and an increment of 2
Vector
<
Integer
vec
=
new
Vector
<>
(
3
,
2
);
System
.
out
.
println
(
"Initial capacity: "
vec.
capacity
(
)
)
;
// 3
vec.
add
(
1
)
;
vec.
add
(
2
)
;
vec.
add
(
3
)
;
vec.
add
(
4
)
;
// Exceeds the initial capacity, auto-grows
System
.
out
.
println
(
"Capacity after growth: "
vec.
capacity
(
)
)
;
// 5 (3 + 2)
Best Practices
- Set a Reasonable Initial Capacity: If the number of elements can be estimated, setting an appropriate initial capacity can reduce the performance overhead of automatic expansion
- Avoid Frequent Expansion: Frequent expansion affects performance, especially when processing large amounts of data
- Use trimToSize() When Appropriate: If you are sure not to add more elements, you can use
trimToSize()to adjust the capacity to the current size and save memory
Example
Vector
<
String
names
=
new
Vector
<>
(
100
);
// Add elements...
names.
trimToSize
(
)
;
// Adjust the capacity to the current size
Common questions
Q1: What is the difference between capacity() and size()?
capacity()returns the total number of elements the Vector can currently storesize()returns the number of elements actually stored in the Vector at present
Q2: Why is my Vector's capacity much larger than the actual number of elements?
This is caused by the Vector's automatic expansion mechanism. When the Vector needs to expand, it adds enough space to avoid frequent expansion, which may cause the capacity to be much larger than the current number of stored elements.
Q3: How can I reduce the capacity of a Vector?
You can use the trimToSize() method to adjust the capacity of the Vector to the current size:
vector.trimToSize();
Summary
capacity() The method is one of the important methods for understanding and using Java Vector. By managing the Vector's capacity reasonably, you can improve program performance and optimize memory usage. Remember, capacity is the size of the Vector's internal storage space, while size is the number of elements actually stored.
Java Vector