Vector trimToSize() method in Java with Example Last Updated : 08 Mar, 2024 Comments Improve Suggest changes Like Article 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 More infoAdvertise with us Next Article Vector trimToSize() method in Java with Example ankit15697 Follow Improve Article Tags : Java Technical Scripter Java-Collections Java - util package Java-Functions Java-Vector +2 More Practice Tags : JavaJava-Collections Similar Reads Vector removeAllElements() method in Java with Example The Java.util.Vector.removeAllElements() method is used to removes all components from this Vector and sets its size to zero. Syntax: Vector.removeAllElements() Parameters: The method does not take any parameter Return Value: The function does not returns any value. Below programs illustrate the Jav 2 min read Vector removeElement() method in Java with Example The java.util.Vector.removeElement() method is used to remove first occurrence of particular object. If object is not found then it returns false else it returns true. If a particular object is present inside vector and removeElement() method call on that vector element then this method reduces vect 3 min read Vector removeElementAt() Method in Java The java.util.vector.removeElementAt(int index) method is used to remove an element from a Vector from a specific position or index. In this process the size of the vector is automatically reduced by one and all other elements after the removed element are shifted downwards by one position. Syntax: 2 min read Vector removeIf() method in Java The removeIf() method of Vector removes all of those elements from Vector which satisfies the condition passed as a parameter to this method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or 3 min read Vector removeRange() method in Java with Example The removeRange() method of Vector in Java is used to remove all elements within the specified range from an Vector object. It shifts any succeeding elements to the left. This call shortens the Vector by (toIndex-fromIndex) elements where toIndex is the ending index and fromIndex is the starting ind 3 min read Vector retainAll() method in Java with Examples The retainAll method of class vector in java is an inbuilt function in Java which is used to retains only the elements in this Vector that are contained in the specified Collection. In other words, removes from this Vector all of its elements that are not contained in the specified Collection. Synta 3 min read Vector setElementAt() method in Java with Example The setElementAt() method of Java Vector is used to set the component at the specified index of this vector to be the specified object. The previous component at that position is discarded. The index must be a value greater than or equal to 0 and less than the current size of the vector. Syntax: pub 2 min read Vector setSize() method in Java with Example The Java.util.Vector.setSize() is a method of Vector class that is used to set the new size of the vector. If the new size of the vector is greater than the current size then null elements are added to the vector is new size is less than the current size then all higher-order elements are deleted. T 3 min read Vector size() Method in Java The java.util.vector.size() method in Java is used to get the size of the Vector or the number of elements present in the Vector. Syntax: Vector.size() Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Vector. Below 2 min read Collections.sort() in Java with Examples java.util.Collections.sort() method is present in java.util.Collections class. It is used to sort the elements present in the specified list of Collection in ascending order. It works similar to java.util.Arrays.sort() method but it is better than as it can sort the elements of Array as well as link 5 min read Like