Java Vector removeElement() Method
removeElement()The method is a member method in Java,Vectorprovided by the class, used to remove the specified element from the vector. This method belongs tojava.util.Vectorthe class, and is part of the Java Collections Framework.
Method Syntax:
public boolean removeElement(Object obj)
Function:
- Removes from the vector the first (smallest-index) element that is
objequal to the specified parameter. - If the vector contains this element, it returns
true - If the vector does not contain this element, it returns
false
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| obj | Object | The element to be removed from the vector |
Return Value
- If the vector contains the specified element and it is successfully removed, returns
true - If the vector does not contain the specified element, returns
false
Method Features
1. Based on equality comparison
The `removeElement()` method uses the `equals()` method to determine whether elements are equal, rather than using the `==` operator.
2. Only removes the first matching item
If the vector contains multiple identical elements, this method only removes the first occurring element (the one with the smallest index).3. Thread-safe
Since `Vector` is thread-safe, the `removeElement()` method is also synchronized and can be safely used in a multi-threaded environment.
Usage Example
Example
public class VectorRemoveExample {
public static void main(String[] args) {
// Create a Vector and add elements
Vector<String> fruits = new Vector<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // Add Apple repeatedly
System.out.println("Original Vector: " + fruits);
// Remove "Banana"
boolean removed = fruits.removeElement("Banana");
System.out.println("Successfully removed Banana: " + removed);
System.out.println("Vector after removing Banana: " + fruits);
// Try to remove a non-existent element
removed = fruits.removeElement("Grape");
System.out.println("Successfully removed Grape: " + removed);
System.out.println("Vector after attempting to remove Grape: " + fruits);
// Remove the first "Apple"
removed = fruits.removeElement("Apple");
System.out.println("Successfully removed Apple: " + removed);
System.out.println("Vector after removing the first Apple: " + fruits);
}
}
Output Result:
原始 Vector: [Apple, Banana, Orange, Apple] 是否成功移除 Banana: true 移除 Banana 后的 Vector: [Apple, Orange, Apple] 是否成功移除 Grape: false 尝试移除 Grape 后的 Vector: [Apple, Orange, Apple] 是否成功移除 Apple: true 移除第一个 Apple 后的 Vector: [Orange, Apple]
Notes
null value handling:
removeElement()The method can handlenullnull values, and can remove thenullelement from the vector.Performance Considerations: Since
Vectorit is implemented based on an array, removing elements (especially non-tail elements) will cause array elements to shift, which may affect performance in large vectors.Difference from remove():
removeElement()The method andremove(Object o)the method have the same functionality, butremoveElement()YesVectoris a method unique to , whileremove(Object o)YesListis a method defined by the interface.Concurrent Modification: If you call this method to modify the vector while iterating over it, it may throw
ConcurrentModificationException。
Related Methods
remove(int index): Removes the element at the specified index positionremoveAllElements(): Removes all elements from the vectorremoveElementAt(int index): Removes the element at the specified index positionremoveRange(int fromIndex, int toIndex): Removes all elements within the specified range
By understanding and usingremoveElement()the method, you can more effectively manipulateVectorthe elements in the collection.
Java Vector