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

Java Vector retainAll() Method

Java Vector retainAll() Method

Java Vector Java Vector


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:

Return Value

This method returns a boolean value:

Method behavior

When retainAll() method, the following operations occur:

  1. The method iterates over all elements in the current vector
  2. For each element, checks whether it exists in the argument collection c Medium
  3. If the element does not exist in c , removes it from the current vector
  4. Finally, the current vector will contain only those elements that exist in both the vector itself and the argument collection c elements 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

  1. Element Comparison in the Collection:retainAll() method uses equals() method to compare whether elements are the same. Therefore, for custom objects, ensure that equals() and hashCode() method.

  2. The argument collection is null: If the argument collection c is null, throws NullPointerException.

  3. Concurrent Modification: If the collection is modified during iteration (except through the iterator's own remove() method), it may throw ConcurrentModificationException.

  4. 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:

  1. Data Filtering: Filter elements that meet specific criteria from a large collection
  2. Set Intersection: Compute the intersection of two sets
  3. 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 Java Vector