YouTip LogoYouTip

Java Vector Removeelement

Java Vector removeElement() Method

\n\n

Java Vector Java Vector

\n\n
\n\n

removeElement() is a member method provided by the Vector class in Java, used to remove a specified element from the vector. This method belongs to the java.util.Vector class and is part of the Java Collections Framework.

\n\n

Method Syntax:

\n
public boolean removeElement(Object obj)
\n\n

Functionality:

\n
    \n
  • Removes the first (lowest index) element equal to the specified parameter obj from the vector
  • \n
  • Returns true if the vector contains the element
  • \n
  • Returns false if the vector does not contain the element
  • \n
\n\n

Parameter Description

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ParameterTypeDescription
objObjectThe element to be removed from the vector
\n\n

Return Value

\n
    \n
  • Returns true if the specified element is found in the vector and successfully removed
  • \n
  • Returns false if the specified element is not found in the vector
  • \n
\n\n
\n\n

Method Features

\n\n

1. Based on Equality Comparison

\n

The removeElement() method uses the equals() method to determine equality between elements, rather than the == operator.

\n\n

2. Removes Only the First Match

\n

If the vector contains multiple identical elements, this method will only remove the first occurrence (the one with the lowest index).

\n\n

3. Thread Safety

\n

Since Vector is thread-safe, the removeElement() method is synchronized and can be safely used in multi-threaded environments.

\n\n
\n\n

Usage Example

\n\n

Example

\n
import java.util.Vector;\n\npublic class VectorRemoveExample {\n\npublic static void main(String[] args){\n\n// Create a Vector and add elements\n\n Vector fruits =new Vector();\n\n fruits.add("Apple");\n\n fruits.add("Banana");\n\n fruits.add("Orange");\n\n fruits.add("Apple");// Repeat adding Apple\n\nSystem.out.println("Original Vector: "+ fruits);\n\n// Remove "Banana"\n\nboolean removed = fruits.removeElement("Banana");\n\nSystem.out.println("Whether successfully removed Banana: "+ removed);\n\nSystem.out.println("Vector after removing Banana: "+ fruits);\n\n// Attempt to remove a non-existent element\n\n removed = fruits.removeElement("Grape");\n\nSystem.out.println("Whether successfully removed Grape: "+ removed);\n\nSystem.out.println("Vector after attempting to remove Grape: "+ fruits);\n\n// Remove the first "Apple"\n\n removed = fruits.removeElement("Apple");\n\nSystem.out.println("Whether successfully removed Apple: "+ removed);\n\nSystem.out.println("Vector after removing the first Apple: "+ fruits);\n\n}\n\n}
\n\n

Output:

\n
Original Vector: [Apple, Banana, Orange, Apple]Whether successfully removed Banana: trueVector after removing Banana: [Apple, Orange, Apple]Whether successfully removed Grape: falseVector after attempting to remove Grape: [Apple, Orange, Apple]Whether successfully removed Apple: trueVector after removing the first Apple: [Orange, Apple]
\n\n
\n\n

Important Notes

\n
    \n
  1. null Value Handling: The removeElement() method can handle null values and can remove null elements from the vector.
  2. \n
  3. Performance Considerations: Since Vector is implemented based on an array, removing elements (especially those not at the end) causes array element shifting, which may impact performance in large vectors.
  4. \n
  5. Difference from remove(): The removeElement() method functions identically to the remove(Object o) method. However, removeElement() is specific to Vector, whereas remove(Object o) is defined by the List interface.
  6. \n
  7. Concurrent Modification: Calling this method to modify the vector during iteration may throw a ConcurrentModificationException.
  8. \n
\n\n
\n\n

Related Methods

\n
    \n
  • remove(int index): Removes the element at the specified index
  • \n
  • removeAllElements(): Removes all elements from the vector
  • \n
  • removeElementAt(int index): Removes the element at the specified index
  • \n
  • removeRange(int fromIndex, int toIndex): Removes all elements within the specified range
  • \n
\n\n

By understanding and using the removeElement() method, you can more effectively manipulate elements within a Vector collection.

\n\n

Java Vector Java Vector

← Java Vector RemoverangeJava Vector Removeall β†’