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

Java Vector removeAll() Method

Java Vector removeAll() Method

Java Vector Java Vector


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

Return Value


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

  1. Batch Operations: Can remove multiple elements at once
  2. Collection-based removal: The removal criterion is based on the contents of another collection
  3. Modifies the original Vector: The method directly modifies the Vector object
  4. 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

  1. Empty Collection Handling: If the argument c is an empty collection, the method does not throw an exception but returns false

Example

Vector<String> v = new Vector<>(Arrays.asList("A", "B", "C"));
boolean result = v.removeAll(new ArrayList<String>());
System.out.println(result);  // Outputs false
2.

null value handling:

- If the argument `c` is `null`, a `NullPointerException` is thrown - If the `Vector` contains `null` elements and the collection `c` also contains `null`, then `null` will be removed
  1. Performance considerations: For large Vector, this method's performance may not be high because its time complexity is O(n*m), where n is the Vector 's size and m is the collection's c size 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

  1. Data Filtering: Remove elements that do not meet the criteria from a data set
  2. Blacklist Processing: Remove blacklisted users from a user list
  3. Data Cleanup: Remove invalid or test data from a data set
  4. 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 Java Vector