Stack trimToSize() method in Java with Example Last Updated : 24 Dec, 2018 Suggest changes Share Like Article Like Report The trimToSize() method of Stack in Java trims the capacity of an Stack instance to be the list's current capacity. This method is used to trim an Stack instance to the number of elements it contains. Syntax: public void trimToSize() Parameter: It does not accepts any parameter. Return Value: It does not returns any value. It trims the capacity of this Stack instance to the number of the element it contains. Below program illustrate the trimToSize() method: Java // Java code to demonstrate the working of // trimToSize() method in Stack // for Stack functions import java.util.Stack; public class GFG { public static void main(String[] args) { // Creating object of Stack<Integer> Stack<Integer> stack = new Stack<Integer>(); // adding element to stack stack.add(10); stack.add(20); stack.add(30); stack.add(40); // Print the Stack System.out.println("Stack: " + stack); // Print the current capacity of Stack System.out.println("Current capacity of Stack: " + stack.capacity()); // Change the capacity to 15 stack.ensureCapacity(15); // Print the current capacity of Stack System.out.println("New capacity of Stack: " + stack.capacity()); // trims the capacity to the number of elements stack.trimToSize(); // Print the current capacity of Stack System.out.println("Current capacity of Stack" + " after use of trimToSize() method: " + stack.capacity()); } } Output: Stack: [10, 20, 30, 40] Current capacity of Stack: 10 New capacity of Stack: 20 Current capacity of Stack after use of trimToSize() method: 4 Example 2: Java // Java program to demonstrate // Stack toString() method import java.util.*; public class collection { public static void main(String args[]) { // Creating an Empty Stack Stack<String> stack = new Stack<String>(); // Use add() method // to add elements to the Collection stack.add("Welcome"); stack.add("To"); stack.add("Geeks"); stack.add("For"); stack.add("Geeks"); // Print the Stack System.out.println("Stack: " + stack); // Print the current capacity of Stack System.out.println("Current capacity of Stack: " + stack.capacity()); // Change the capacity to 20 stack.ensureCapacity(20); // Print the current capacity of Stack System.out.println("New capacity of Stack: " + stack.capacity()); // trims the capacity to the number of elements stack.trimToSize(); // Print the current capacity of Stack System.out.println("Current capacity of Stack" + " after use of trimToSize() method: " + stack.capacity()); } } Output: Stack: [Welcome, To, Geeks, For, Geeks] Current capacity of Stack: 10 New capacity of Stack: 20 Current capacity of Stack after use of trimToSize() method: 5 C code_r Follow Article Tags : Java Java-Collections Java - util package Java-Functions Java-Stack +1 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 Constructors10 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 My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like