Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Vector removeElement() Method

Java Vector removeElement() Method

Java Vector Java Vector


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:

Parameter Description

Parameters Type Description
obj Object The element to remove from the vector

Return Value


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

  1. null Value Handling:removeElement() The method can handle null values and can remove null .

  2. Performance considerations: Because Vector is implemented based on arrays; removing an element (especially a non-final element) causes array elements to shift, which may affect performance in large vectors.

  3. Differences from remove():removeElement() The method and the remove(Object o) methods have the same function, but removeElement() Is Vector is a method specific to, while remove(Object o) Is List is a method defined by the interface.

  4. Concurrent Modification: If this method is called to modify the vector during iteration, it may throw ConcurrentModificationException.


Related methods

By understanding and using removeElement() method, you can more effectively operate on Vector elements in the collection.

Java Vector Java Vector