Vector trimToSize() method in Java with Example Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 2 Likes Like Report 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: Java // 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()); } } Output:Initial Vector is: [2, 4, 5, 6, 11] Initial size is: 5 Size after using trimToSize(): 5 Example 2: Java // 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()); } } Output:Initial Vector is: [Geeks, 4, Geeks, Welcomes, You] Initial size is: 5 Size after using trimToSize(): 5 Time complexity: O(n), here n is the number of elements in the vector.Auxiliary space: O(n). Comment A ankit15697 Follow 2 Improve A ankit15697 Follow 2 Improve Article Tags : Java Technical Scripter Java-Collections Java - util package Java-Functions Java-Vector +2 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like