Java ArrayList trimToSize() Method

The ArrayList.trimToSize() method in Java is used to trim the capacity of the ArrayList instance to be the list's current size. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. trimToSize Method Syntax
  3. Examples
    • Trimming Capacity to Size
    • Comparing Capacity Before and After Trimming
  4. Conclusion

Introduction

The ArrayList.trimToSize() method is a member of the ArrayList class in Java. It is used to minimize the storage of an ArrayList instance. By calling this method, you can ensure that the capacity of the ArrayList is equal to its current size, thereby reducing memory usage.

trimToSize Method Syntax

The syntax for the trimToSize method is as follows:

public void trimToSize()
  • This method does not take any parameters and does not return any value.

Examples

Trimming Capacity to Size

The trimToSize method can be used to trim the capacity of an ArrayList to its current size, removing any unused capacity.

Example

import java.util.ArrayList;

public class TrimToSizeExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(10); // Initial capacity of 10
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        System.out.println("ArrayList before trimToSize: " + list);

        // Trim the capacity of the ArrayList to its current size
        list.trimToSize();

        System.out.println("ArrayList after trimToSize: " + list);
    }
}

Output:

ArrayList before trimToSize: [Apple, Banana, Orange]
ArrayList after trimToSize: [Apple, Banana, Orange]

Comparing Capacity Before and After Trimming

To illustrate the effect of trimToSize, we can use reflection to check the capacity of the ArrayList before and after trimming. This requires accessing the private elementData field of the ArrayList class.

Example

import java.util.ArrayList;
import java.lang.reflect.Field;

public class TrimToSizeCapacityExample {
    public static void main(String[] args) throws Exception {
        ArrayList<String> list = new ArrayList<>(10); // Initial capacity of 10
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        // Print capacity before trimToSize
        System.out.println("Capacity before trimToSize: " + getCapacity(list));

        // Trim the capacity of the ArrayList to its current size
        list.trimToSize();

        // Print capacity after trimToSize
        System.out.println("Capacity after trimToSize: " + getCapacity(list));
    }

    private static int getCapacity(ArrayList<?> list) throws Exception {
        Field dataField = ArrayList.class.getDeclaredField("elementData");
        dataField.setAccessible(true);
        Object[] elementData = (Object[]) dataField.get(list);
        return elementData.length;
    }
}

Output:

Capacity before trimToSize: 10
Capacity after trimToSize: 3

Conclusion

The ArrayList.trimToSize() method in Java is a useful tool for optimizing the memory usage of an ArrayList by trimming its capacity to its current size. By understanding how to use this method, you can ensure efficient memory management in your Java applications. Whether you are working with a large list or a small one, the trimToSize method helps to minimize the storage overhead associated with an ArrayList.

Comments