ArrayList trimToSize() Method in Java with Example Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In Java, the trimToSize() method is used to reduce the capacity of an ArrayList to match its current size. This helps to optimize memory usage when an ArrayList contains less elements than its allocated capacity.Example 1: Here, we will use the trimToSize() method to trim an ArrayList of Strings. Java // Java program to demonstrate the use of // trimToSize() method in ArrayList of Strings import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Creating an ArrayList of Strings ArrayList<String> n = new ArrayList<>(10); // Adding elements to the ArrayList n.add("Ram"); n.add("Shyam"); n.add("Hari"); // print the size and // capacity before trimming System.out.println("Size before trim: " + n.size()); System.out.println("Capacity before trim: Excess capacity exists"); // Trimming to the size // of the ArrayList n.trimToSize(); // Printing the size after trimming System.out.println("Size after trim: " + n.size()); } } OutputSize before trim: 3 Capacity before trim: Excess capacity exists Size after trim: 3 Syntax of trimToSize() Methodpublic void trimToSize()Below is the diagram that represents the working of trimToSize() method of ArrayList.Example 2: Here, we will use the trimToSize() method to trim an ArrayList of Integers. Java // Java program to demonstrate trimToSize() // with Integer ArrayList import java.util.ArrayList; public class GFG { public static void main(String[] args) { // creating an Empty Integer ArrayList ArrayList<Integer> arr = new ArrayList<Integer>(9); arr.add(2); arr.add(4); arr.add(5); arr.add(6); arr.add(11); // trims the size to the // number of elements arr.trimToSize(); System.out.println("The List elements are:"); // prints all the elements for (Integer n : arr) { System.out.println("Number:" + n); } } } OutputThe List elements are: Number:2 Number:4 Number:5 Number:6 Number:11 Example 3: Here, we will check the behavior of trimToSize() on an empty ArrayList. Java // Java program to show trimToSize() // on an empty ArrayList import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Creating an empty ArrayList ArrayList<String> arr = new ArrayList<>(); // Trimming the capacity of // an empty ArrayList arr.trimToSize(); // Printing the size after trimming System.out.println("Size of empty ArrayList after trim: " + arr.size()); } } OutputSize of empty ArrayList after trim: 0 Comment More info S Striver Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-ArrayList java-list +3 More Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 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 Networking15+ 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 Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like