Vector trimToSize() method in Java with Example

The trimToSize() method of Vector in Java trims the capacity of an Vector instance to be the Vector’s current size. This method is used to trim an Vector instance to the number of elements it contains.

Syntax:

trimToSize()

Parameter: It does not accepts any parameter.

Return Value: It does not returns any value. It trims the capacity of this Vector instance to the number of the element it contains.

Below program illustrate the trimTosize() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the working of
// trimTosize() method in Vector
  
import java.util.Vector;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // creating an Empty Integer Vector
        Vector<Integer> vector
            = new Vector<Integer>(9);
  
        // using add(), add 5 values
        vector.add(2);
        vector.add(4);
        vector.add(5);
        vector.add(6);
        vector.add(11);
  
        // Displaying the Vector
        System.out.println("Initial Vector is: " + vector);
  
        // Displaying the Vector
        System.out.println("Initial size is: " + vector.size());
  
        // trims the size to the number of elements
        vector.trimToSize();
  
        // Displaying the Vector
        System.out.println("Size after using trimToSize(): "
                           + vector.size());
    }
}

chevron_right


Output:

Initial Vector is: [2, 4, 5, 6, 11]
Initial size is: 5
Size after using trimToSize(): 5

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the working of
// trimTosize() method in Vector
  
import java.util.Vector;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // creating vector type object
        Vector<String> vector = new Vector<String>();
  
        // Inserting elements into the table
        vector.add("Geeks");
        vector.add("4");
        vector.add("Geeks");
        vector.add("Welcomes");
        vector.add("You");
  
        // Displaying the Vector
        System.out.println("Initial Vector is: " + vector);
  
        // Displaying the Vector
        System.out.println("Initial size is: " + vector.size());
  
        // trims the size to the number of elements
        vector.trimToSize();
  
        // Displaying the Vector
        System.out.println("Size after using trimToSize(): "
                           + vector.size());
    }
}

chevron_right


Output:

Initial Vector is: [Geeks, 4, Geeks, Welcomes, You]
Initial size is: 5
Size after using trimToSize(): 5


My Personal Notes arrow_drop_up

All powers are within you You can do anything and everything Believe in that

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.