Java Vector - trimToSize() Method
The java.util.Vector.trimToSize() method is used to trim the capacity of the vector to be the vector's current size. If the capacity of this vector is larger than its current size, then the capacity is changed to equal the size.
Syntax
public void trimToSize()
Parameters
No parameter is required.
Return Value
void type.
Exception
NA.
Example:
In the example below, the java.util.Vector.trimToSize() method is used to trim the capacity of the vector to be the vector's current size.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a vector Vector<Integer> Vec = new Vector<Integer>(10); //populating vector Vec.add(10); Vec.add(20); Vec.add(30); //print the capacity of the vector System.out.println("Before trimToSize, Vec capacity: "+ Vec.capacity()); //trim the capacity of the vector Vec.trimToSize(); //print the capacity of the vector System.out.println("After trimToSize, Vec capacity: "+ Vec.capacity()); } }
The output of the above code will be:
Before trimToSize, Vec capacity: 10 After trimToSize, Vec capacity: 3
❮ Java.util - Vector