Vector trimToSize() in Java

trimToSize(): This method is available in java.util.Vector class of Java.

Syntax:

void java.util.Vector.trimToSize()

This method trims the capacity of this vector to be the vector's current size.

Note:

1. If the capacity of this vector is larger than its current size, then the capacity is changed to equal the size by replacing its internal data array, kept in the field element Data, with a smaller one.

2. An application can use this operation to minimize the storage of a vector.

Parameters: NA

Returns: NA

Exceptions: NA

Approach

Java

import java.util.Vector;

public class VectortrimToSize {
    public static void main(String[] args) {

        Vector<String> vector = new Vector<>();

        vector.add("Hello");
        vector.add("Java");
        vector.add("C++");
        vector.add("Program");

        vector.trimToSize();
        System.out.println(vector);
    }
}

Output:

[Hello, Java, C++, Program]


No comments:

Post a Comment