This example shows how to clear or remove all elements from the Vector in Java. This example also shows how to clear or remove all Vector elements using clear, removeAllElements, and setSize methods.
How to clear or remove all elements from Vector in Java?
There are several ways using which we can remove all elements from the vector object as given below.
1. Using the clear method
The Vector clear
method removes all elements from this vector object.
1 |
public void clear() |
The vector object becomes empty after this method call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Vector; public class ClearVectorExample { public static void main(String[] args) { Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); /* * To delete or remove all elements from the vector, * use the clear method */ vNumbers.clear(); System.out.println("Is empty? " + vNumbers.isEmpty()); } } |
Output
1 |
Is empty? true |
Please also visit how to check if the vector is empty example to know more.
2. Using the removeAllElements method
The Vector removeAllElements
method is similar to the clear
method and removes all elements from this vector object.
1 |
public void removeAllElements() |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); /* * We can also use the removeAllElements * method to empty the vector object */ vNumbers.removeAllElements(); System.out.println("Is empty? " + vNumbers.isEmpty()); |
Output
1 |
Is empty? true |
3. Using the setSize method
The Vector setSize
method sets the size of this vector object to the specified new size.
1 |
public void setSize(int newSize) |
To clear or empty the vector object, we will pass 0 to this method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); /* * To empty the vector, we will pass * 0 to the setSize method */ vNumbers.setSize(0); System.out.println("Is empty? " + vNumbers.isEmpty()); |
Output
1 |
Is empty? true |
Which method should I use?
Let’s have a look at the source code of all these Vector methods.
1 2 3 |
public void clear() { removeAllElements(); } |
1 2 3 4 5 6 7 8 |
public synchronized void removeAllElements() { modCount++; // Let gc do its work for (int i = 0; i < elementCount; i++) elementData[i] = null; elementCount = 0; } |
1 2 3 4 5 6 7 8 9 10 11 |
public synchronized void setSize(int newSize) { modCount++; if (newSize > elementCount) { ensureCapacityHelper(newSize); } else { for (int i = newSize ; i < elementCount ; i++) { elementData[i] = null; } } elementCount = newSize; } |
As we can see from the source code, the clear
method simply calls the removeAllElements
method. The removeAllElements
method and setSize
method iterates through the vector elements and sets them to null.
As far as performance is concerned, all three will more or less perform the same. Using the clear
method is a preferred approach just because it conforms to the new JDK standard.
Please also visit how to remove elements from Vector in Java example to know more.
This example is a part of the Vector in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Vector Documentation