Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Vector ensureCapacity() Method

Java Vector ensureCapacity() Method

Java Vector Java Vector

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

Return Value

The method has no return value (void type)


Method Effect

ensureCapacity() The main effects of the method are:

  1. If the current Vector 's capacity is less than the specified minimum capacity, increase its capacity
  2. If the current capacity is already large enough, do nothing
  3. 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

  1. Checks whether the current capacity is less than the minimum capacity specified by the parameter
  2. If so, creates a new, larger array
  3. Copies the existing elements into the new array
  4. Updates the internal reference to point to the new array

Performance considerations

Why Use ensureCapacity()

Expansion Strategy

If you do not use ensureCapacity(), Vector has its own expansion strategy:

  1. By default,Vector 's initial capacity is 10
  2. When expansion is needed, the capacity doubles by default (i.e., new capacity = old capacity * 2)
  3. A different growth amount can also be specified

Notes

  1. ensureCapacity() Only increases capacity, never decreases it
  2. If the specified minimum capacity is less than the current capacity, the method does nothing
  3. This method does not change the Vector 's size(); it only affects capacity()
  4. 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 Java Vector