Java Vector removeElement() Method

Java Vector Java Vector


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 isobjequal to the specified parameter.
  • If the vector contains this element, it returnstrue
  • If the vector does not contain this element, it returnsfalse

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, returnstrue
  • If the vector does not contain the specified element, returnsfalse

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

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("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

  1. null value handling:removeElement()The method can handlenullnull values, and can remove thenullelement from the vector.

  2. Performance Considerations: SinceVectorit 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.

  3. 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.

  4. Concurrent Modification: If you call this method to modify the vector while iterating over it, it may throwConcurrentModificationException。


Related Methods

  • remove(int index): Removes the element at the specified index position
  • removeAllElements(): Removes all elements from the vector
  • removeElementAt(int index): Removes the element at the specified index position
  • removeRange(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 Java Vector

Other Extensions