trimToSize() method

trimToSize(): This trimtosize in Java method is used to trim the capacity of the instance of the ArrayList to the list’s current size.

void trimToSize()

It doesn’t return anything, So its return type is void.

import java.util.ArrayList;
public class ExampleOfArrayList 
{
	public static void main(String arg[])
	{
		ArrayList<String> listOfNames = new ArrayList<String>();
		listOfNames.add("RAVI");
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("SITE");
		
	    listOfNames.trimToSize();
	    
	    for(String name : listOfNames)
	    	System.out.println(name);
	}
}

Output: RAVI
JAVA
GOAL
SITE

Leave a Comment