Java Vector removeElement() Method
removeElement() The method is in Java's Vector A member method provided by the class, used to remove the specified element from the vector. This method belongs to java.util.Vector class and is part of the Java Collections Framework.
Method Syntax:
public boolean removeElement(Object obj)
Function:
- Removes from the vector the first (lowest-indexed) element equal to the specified
objargument - If the vector contains the element, returns
true - If the vector does not contain the element, returns
false
Parameter Description
| Parameters | Type | Description |
|---|---|---|
| obj | Object | The element to remove 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 Characteristics
1. Based on Equality Comparison
removeElement() method uses equals() method to determine whether elements are equal, rather than using == new operator.
2. Removes Only the First Match
If the vector contains multiple identical elements, this method removes only the first occurrence (the one with the lowest index).
3. Thread safety
Because Vector is thread-safe,removeElement() The method is also synchronized and can be used safely in a multithreaded environment.
Usage examples
Example
import
java.util.Vector
;
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
(
"Was Banana successfully removed: "
removed
)
;
System
.
out
.
println
(
"Vector after removing Banana: "
fruits
)
;
// Try to remove a non-existent element
removed
=
fruits.
removeElement
(
"Grape"
)
;
System
.
out
.
println
(
"Was Grape successfully removed: "
removed
)
;
System
.
out
.
println
(
"Vector after attempting to remove Grape: "
fruits
)
;
// Remove the first "Apple"
removed
=
fruits.
removeElement
(
"Apple"
)
;
System
.
out
.
println
(
"Was Apple successfully removed: "
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 handlenullvalues and can removenull. -
Performance considerations: Because
Vectoris implemented based on arrays; removing an element (especially a non-final element) causes array elements to shift, which may affect performance in large vectors. -
Differences from remove():
removeElement()The method and theremove(Object o)methods have the same function, butremoveElement()IsVectoris a method specific to, whileremove(Object o)IsListis a method defined by the interface. -
Concurrent Modification: If this method is called to modify the vector during iteration, it may throw
ConcurrentModificationException.
Related methods
remove(int index): Removes the element at the specified index positionremoveAllElements(): Removes all elements in 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 using removeElement() method, you can more effectively operate on Vector elements in the collection.
Java Vector