ArrayList trimToSize() Method in Java with Example Last Updated : 10 Dec, 2024 Summarize Comments Improve Suggest changes Share 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 infoAdvertise with us Next Article ArrayList trimToSize() Method in Java with Example S Striver Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-ArrayList java-list +3 More Practice Tags : JavaJava-CollectionsMisc Similar Reads ArrayList in Java Java ArrayList is a part of the collections framework and it is a class of java.util package. It provides us with dynamic-sized arrays in Java. The main advantage of ArrayList is that, unlike normal arrays, we don't need to mention the size when creating ArrayList. It automatically adjusts its capac 9 min read ArrayList and LinkedList remove() methods in Java with Examples List interface in Java (which is implemented by ArrayList and LinkedList) provides two versions of remove method. boolean remove(Object obj) : It accepts object to be removed. It returns true if it finds and removes the element. It returns false if the element to be removed is not present. Removes t 3 min read ArrayList get(index) Method in Java with Examples The get(index) method of ArrayList in Java is used to retrieve the element at the specified index within the list.Example 1: Here, we will use the get() method to retrieve an element at a specific index in an ArrayList of integers.Java// Java program to demonstrate the working of // get() method in 2 min read Java ArrayList add() Method with Examples The add() method in the ArrayList class is used to add elements to the list. There are different versions of this method. Example 1: In this example, we will use the add() method to add elements at the end of the list.Java// Java Program to demonstrate Addition of // Elements to an ArrayList import 2 min read Java ArrayList addall() Method with Example The addAll() method in the ArrayList class is used to add all the elements from a specified collection into an ArrayList. This method is especially useful for combining collections or inserting multiple elements at once.Example: Here, we will add elements to the end of the list.Java// Java program t 2 min read ArrayList clear() Method in Java with Examples The clear() method in the ArrayList class in Java is used to remove all elements from an ArrayList. After calling this method, the list becomes empty.Example 1: Here, we will use the clear() method to clear elements from an ArrayList of Integers.Java// Java program to demonstrate clear() method // i 2 min read clone() Method - Object Cloning in Java Object cloning in Java refers to creating an exact copy of an object. The clone() method in Java is used to clone an object. It creates a new instance of the class of the current object and initializes all its fields with exactly the contents of the corresponding fields of this object.Example 1: Her 5 min read Arraylist.contains() Method in Java In Java, the ArrayList contains() method is used to check if the specified element exists in an ArrayList or not. Example: Here, we will use the contains() method to check if the ArrayList of Strings contains a specific element.Java// Java program to demonstrate the use of contains() // method with 2 min read ArrayList ensureCapacity() Method in Java with Examples In Java, the ArrayList.ensureCapacity() method is used to increase the internal capacity of an ArrayList to ensure that it can hold a specified minimum number of elements. It helps to optimize performance by reducing the number of dynamic reallocations when adding a large number of elements.Example 2 min read ArrayList forEach() Method in Java In Java, the ArrayList.forEach() method is used to iterate over each element of an ArrayList and perform certain operations for each element in ArrayList.Example 1: Here, we will use the forEach() method to print all elements of an ArrayList of Strings.Java// Java program to demonstrate the use of f 2 min read Like