Java Vector retainAll() Method

Java Vector Java Vector


retainAll()The method is aVectorcollection operation method provided by the class, which is 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 elements common to both the vector and the specified collection.

Method syntax:

public boolean retainAll(Collection<?> c)

Method Parameters

retainAll()The method accepts one parameter:

  • Collection<?> cA collection containing the elements to be retained. This collection can be any object that implements theCollectioninterface, such asArrayList、HashSetetc.

Return Value

This method returns a boolean value:

  • truetrue: if the current vector was changed as a result of calling this method (i.e., elements were removed)
  • falsefalse: if the current vector was not changed (i.e., all elements already existed in the specified collection)

Method Behavior

When callingretainAll()the method, the following operations are performed:

  1. The method traverses all elements in the current vector.
  2. For each element, it checks whether the element exists in the parameter collection.cMedium
  3. If the element does not exist incthe collection, it is removed from the current vector.
  4. Finally, the current vector will only contain those elements that exist in both the vector itself and the parameterccollection.

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 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 operation: " + vector1);
    }
}

Output

原始 Vector: [Apple, Banana, Orange, Grape]
要保留的元素集合: [Banana, Grape, Mango]
方法返回值: true
操作后的 Vector: [Banana, Grape]

Notes

  1. Element comparison in the collection:retainAll()The method usesequals()the equals method to compare whether elements are the same. Therefore, for custom objects, ensure that you have correctly implemented theequals()andhashCode()method.

  2. Parameter collection is null: If the parameter collectioncis null, a NullPointerException will be thrown.NullPointerException。

  3. Concurrent Modification: If the collection is modified during iteration (except through the iterator's ownremove()method), a ConcurrentModificationException may be thrown.ConcurrentModificationException。

  4. Performance Considerations: For large collections, the performance of this method may be low because it needs to traverse the entire collection and perform multiple lookups.


Practical Application Scenarios

retainAll()This method is particularly useful in the following scenarios:

  1. Data filtering: Filtering out elements that meet specific conditions from a large collection.
  2. Collection intersection: Calculating the intersection of two collections.
  3. Permission control: Retaining the permissions currently available in the system from the user permission list.

Comparison with Related Methods

Method Description Modifies the current collection Meaning of return value
retainAll() Retains elements that are the same as the specified collection Yes Whether it was changed
removeAll() Removes elements that are the same as the specified collection Yes Whether it was changed
containsAll() Checks whether it contains all elements of the specified collection no Whether all are contained
addAll() Adds all elements of the specified collection Yes Whether it was changed

Summary

Vector.retainAll()is a powerful collection operation method that can effectively filter out elements common to two collections. Understanding how this method works and its applicable scenarios can help developers handle collection operations more efficiently. Remember to handle the return value correctly and choose whether to use this method based on actual needs.

Java Vector Java Vector

Other Extensions