Vector trimToSize() method in Java

trimToSize() method is used to trim the capacity of the vector to its current size. It can be used to minimize the storage of the vector but it can also increase the amount of incremental reallocation and hence can decrease the performance.

  • Method declaration – public synchronized void trimToSize()
  • What does it do? – It trims the capacity of the vector to its current size. So, after using thi method, the capacity of the vector will be equal to the size of the vector.
  • What does it return? – It does not return anything back as its return type is void.

An initial capacity of 10 is assigned to the vector when we don’t explicitly assign any capacity but after using the trimToSize() method, that capacity will be reduced to its size which is illustrated in the program below.

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();

		// adding elements
		v.add("first");
		v.add("second");

		System.out.println("capacity before using trimToSize() method: " + v.capacity());

		v.trimToSize(); // trimming the capacity to its size

		System.out.println("capacity after using trimToSize() method: " + v.capacity());

	}
}

Output –

capacity before using trimToSize() method: 10
capacity after using trimToSize() method: 2

Let’s assign an initial capacity and then try to use the trimToSize() method

We can assign an initial capacity by passing one parameter while making the vector object. If you want to know more about it, then you can go to this link and know about various constructors of the Vector class.

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>(5); // assigning an initial capacity of 5

		// adding elements
		v.add("first");
		v.add("second");

		System.out.println("capacity before using trimToSize() method: " + v.capacity());

		v.trimToSize(); // trimming the capacity to its size

		System.out.println("capacity after using trimToSize() method: " + v.capacity());

	}
}

Output –

capacity before using trimToSize() method: 5
capacity after using trimToSize() method: 2

The What If Scenarios

What if we try to use the method on an empty vector?

In this case, the capacity of the vector will be reduced to zero.

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();

		System.out.println("capacity before using trimToSize() method: " + v.capacity());

		v.trimToSize(); // trimming the capacity to its size

		System.out.println("capacity after using trimToSize() method: " + v.capacity());

	}
}

Output –

capacity before using trimToSize() method: 10
capacity after using trimToSize() method: 0
What if we try to use the method on a null vector?

Here, we will get a NullPointerException as illustrated by the below program.

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();

		v = null;

		System.out.println("capacity before using trimToSize() method: " + v.capacity());

		v.trimToSize(); // trimming the capacity to its size

		System.out.println("capacity after using trimToSize() method: " + v.capacity());

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Vector.capacity()" because "v" is null

Please visit this link if you want to know more about the Vector class in java and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.