Java Vector retainAll() Method
retainAll() The method is in Java's Vector A collection operation method provided by the class, used to retain the elements in the current vector that are the same as those in the specified collection and remove all other elements. In other words, this method modifies the current vector so that it contains only the elements shared with the specified collection.
Method syntax:
public boolean retainAll(Collection> c)
Method parameters
retainAll() method accepts one parameter:
Collection> c: The collection containing the elements to retain. This collection can be any implementation ofCollectioninterface, such asArrayList,HashSet, etc.
Return Value
This method returns a boolean value:
true: If the current vector changed because of calling this method (i.e., some elements were removed)false: If the current vector did not change at all (i.e., all elements already exist in the specified collection)
Method behavior
When retainAll() method, the following operations occur:
- The method iterates over all elements in the current vector
- For each element, checks whether it exists in the argument collection
cMedium - If the element does not exist in
c, removes it from the current vector - Finally, the current vector will contain only those elements that exist in both the vector itself and the argument collection
celements in the
Example Code
Example
import
java.util.Vector
;
import
java.util.Arrays
;
public
class
VectorRetainAllExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create the first Vector
Vector
<
String
vector1
=
new
Vector
<>
(
Arrays.
asList
(
"Apple"
,
"Banana"
,
"Orange"
,
"Grape"
)
)
;
// Create a second collection (using a List here)
java.
util
.
List
<
String
list
=
Arrays.
asList
(
"Banana"
,
"Grape"
,
"Mango"
)
;
System
.
out
.
println
(
"Original Vector: "
vector1
)
;
System
.
out
.
println
(
"Collection of elements to retain: "
list
)
;
// Call the retainAll() method
boolean
changed
=
vector1.
retainAll
(
list
)
;
System
.
out
.
println
(
"Method return value: "
changed
)
;
System
.
out
.
println
(
"Vector after the operation: "
vector1
)
;
}
}
Output result
原始 Vector: [Apple, Banana, Orange, Grape]
要保留的元素集合: [Banana, Grape, Mango]
方法返回值: true
操作后的 Vector: [Banana, Grape]
Notes
-
Element Comparison in the Collection:
retainAll()method usesequals()method to compare whether elements are the same. Therefore, for custom objects, ensure thatequals()andhashCode()method. -
The argument collection is null: If the argument collection
cis null, throwsNullPointerException. -
Concurrent Modification: If the collection is modified during iteration (except through the iterator's own
remove()method), it may throwConcurrentModificationException. -
Performance considerations: For large collections, this method's performance may be low because it needs to traverse the entire collection and perform multiple lookups.
Practical Application Scenarios
retainAll() The method is especially useful in the following scenarios:
- Data Filtering: Filter elements that meet specific criteria from a large collection
- Set Intersection: Compute the intersection of two sets
- Permission Control: Retain the permissions currently available in the system from the user's permission list
Comparison with Related Methods
| Method | Description | Modifies the Current Collection | Meaning of the Return Value |
|---|---|---|---|
retainAll() |
Retain the elements that are the same as those in the specified collection | Is | Whether a change occurred |
removeAll() |
Remove the elements that are the same as those in the specified collection | Is | Whether a change occurred |
containsAll() |
Check whether all elements of the specified collection are contained | No | Whether all are contained |
addAll() |
Add all elements of the specified collection | Is | Whether a change occurred |
Summary
Vector.retainAll() is a powerful collection operation method that can effectively filter out the elements shared by two collections. Understanding how this method works and its applicable scenarios can help developers handle collection operation tasks more efficiently. Remember to correctly handle the method's return value and decide whether to use this method based on actual needs.
Java Vector