Java Vector removeAll() Method
removeAll() The method is in Java's Vector A commonly used method provided by the class, used to remove from the current Vector all elements contained in the specified collection. This method is inherited from AbstractCollection class and is implemented in Vector class.
Method Syntax
public boolean removeAll(Collection> c)
Parameters
c: The collection containing the elements to remove from thisVectorcollection.
Return Value
- If both the
Vectoris changed because of calling this method, returnstrue - If both the
Vectordid not change (i.e., no element in the specified collection exists in theVector), returnsfalse
Detailed Method Functionality
removeAll() The main function of the method is to remove from the Vector elements in bulk. It checks Vector each element in the, and if that element exists in the c specified collection, removes it from the Vector .
Key Features
- Batch Operations: Can remove multiple elements at once
- Collection-based removal: The removal criterion is based on the contents of another collection
- Modifies the original
Vector: The method directly modifies theVectorobject - Preserves Order: After removing elements, the order of the remaining elements stays the same
Usage examples
Example 1: Basic usage
Example
import
java.util.Vector
;
import
java.util.Arrays
;
public
class
VectorRemoveAllExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a Vector
Vector
<
String
vector
=
new
Vector
<>
(
)
;
vector.
add
(
"Apple"
)
;
vector.
add
(
"Banana"
)
;
vector.
add
(
"Orange"
)
;
vector.
add
(
"Grape"
)
;
vector.
add
(
"Mango"
)
;
// Create the collection of elements to remove
Vector
<
String
toRemove
=
new
Vector
<>
(
Arrays
.
asList
(
"Banana"
,
"Grape"
)
)
;
// Use the removeAll() method
boolean
changed
=
vector.
removeAll
(
toRemove
)
;
System
.
out
.
println
(
"Was the Vector modified: "
changed
)
;
System
.
out
.
println
(
"Vector after modification: "
vector
)
;
}
}
Output result:
Vector 是否被修改: true
修改后的 Vector: [Apple, Orange, Mango]
Example 2: Removing All Elements
Example
import
java.util.Vector
;
import
java.util.Arrays
;
public
class
RemoveAllElements
{
public
static
void
main
(
String
[
]
args
)
{
Vector
<
Integer
numbers
=
new
Vector
<>
(
Arrays
.
asList
(
1
,
2
,
3
,
4
,
5
)
)
;
// Create a collection containing all elements
Vector
<
Integer
allElements
=
new
Vector
<>
(
numbers
)
;
// Remove all elements
numbers.
removeAll
(
allElements
)
;
System
.
out
.
println
(
"Vector after clearing: "
numbers
)
;
}
}
Output result:
清空后的 Vector: []
Notes
-
Empty Collection Handling: If the argument
cis an empty collection, the method does not throw an exception but returnsfalse
Example
boolean result = v.removeAll(new ArrayList<String>());
System.out.println(result); // Outputs false
null value handling:
- If the argument
cisnull, will throwNullPointerException - If both the
Vectorcontainsnullelements, and the collectioncalso containsnull, thennullwill be removed
-
Performance considerations: For large
Vector, this method's performance may not be high because its time complexity is O(n*m), where n is theVector's size and m is the collection'scsize of the
Comparison with Related Methods
| Method | Description | Parameters | Return Value |
|---|---|---|---|
removeAll(Collection) |
Removes all elements contained in the specified collection | Union | boolean |
retainAll(Collection) |
Retains all elements contained in the specified collection and removes the others | Union | boolean |
clear() |
Remove all elements | None | void |
remove(Object) |
Removes a single specified element | object | boolean |
remove(int) |
Removes the element at the specified index | Index | The removed element |
Practical Application Scenarios
- Data Filtering: Remove elements that do not meet the criteria from a data set
- Blacklist Processing: Remove blacklisted users from a user list
- Data Cleanup: Remove invalid or test data from a data set
- Set Operations: Implement the set difference operation
Practical Example: Filtering Invalid Data
Example
import
java.util.Vector
;
import
java.util.Arrays
;
public
class
DataFilter
{
public
static
void
main
(
String
[
]
args
)
{
Vector
<
String
rawData
=
new
Vector
<>
(
Arrays
.
asList
(
"valid1"
,
"invalid"
,
"valid2"
,
"error"
,
"valid3"
,
"corrupted"
)
)
;
Vector
<
String
invalidPatterns
=
new
Vector
<>
(
Arrays
.
asList
(
"invalid"
,
"error"
,
"corrupted"
)
)
;
// Remove all invalid data
rawData.
removeAll
(
invalidPatterns
)
;
System
.
out
.
println
(
"Filtered data: "
rawData
)
;
}
}
Output result:
过滤后的数据: [valid1, valid2, valid3]
Summary
Vector.removeAll() is a practical bulk removal method that makes removing multiple elements from a collection simple and efficient. Understanding and correctly using this method can help you write more concise and efficient Java code. Remember its behavior characteristics and caveats when using it to avoid common errors and performance issues.
Java Vector