Java Vector containsAll() Method

Java Vector Java Vector

containsAll()The method is, in Java,Vectora collection operation method provided by the class, used to check whether the currentVectorcontains all elements of the specified collection. It isCollectiona method inherited from the interface, implemented in theVectorclass.

Syntax Format

public boolean containsAll(Collection<?> c)

Method Parameters

Parameter Type Description
c Collection<?> The collection of elements to be checked whether they are contained in the current Vector

Return Value

This method returns a boolean value:

  • truetrue: if the current Vector contains all elements of the specified collection
  • falsefalse: if the current Vector does not contain all elements of the specified collection

Method Characteristics

  1. Complete containment checkReturns true only when the Vector contains all elements in the collection
  2. Empty collection handlingIf the passed-in collection is empty, the method returns true
  3. Element order irrelevantDoes not care about the order of elements in the collection
  4. Duplicate element handlingConsiders the number of duplicate occurrences of elements

Usage Examples

Example 1: Basic Usage

Example

import java.util.Vector;
import java.util.Arrays;

public class VectorContainsAllExample {
    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");
       
        // Create a collection to check
        List<String> checkList = Arrays.asList("Apple", "Banana");
       
        // Use the containsAll() method
        boolean result = vector.containsAll(checkList);
       
        System.out.println("Does the Vector contain all specified elements? " + result);  // Output: true
    }
}

Example 2: Partial Non-containment

Example

Vector<Integer> numbers = new Vector<>(Arrays.asList(1, 2, 3, 4, 5));
List<Integer> checkNumbers = Arrays.asList(2, 6);

boolean result = numbers.containsAll(checkNumbers);
System.out.println(result);  // Output: false, because 6 is not in the Vector

Example 3: Empty Collection Case

Example

Vector<String> fruits = new Vector<>(Arrays.asList("Apple", "Banana"));
List<String> emptyList = new ArrayList<>();

boolean result = fruits.containsAll(emptyList);
System.out.println(result);  // Output: true, any collection contains the empty collection

Important Notes

  1. Performance considerationsFor large collections, this method may require O(n) time complexity
  2. null handling:
    • If the passed-in collection parameter is null, a NullPointerException is thrown
    • Vector can contain null elements, and null elements are also considered during the check
  3. Equality determinationUses the element's equals() method to determine containment

Comparison with Related Methods

Method Description Difference from containsAll()
contains() Checks whether a single element exists Only checks one element rather than a collection
retainAll() Retains elements in the specified collection Modifies the original collection
removeAll() Removes elements in the specified collection Modifies the original collection

Practical Application Scenarios

  1. Permission checkingVerify whether the user has all required permissions
  2. Shopping cart functionalityCheck whether inventory contains all items in the shopping cart
  3. Course selectionVerify whether the student has completed all required courses

Example

// Practical application example: permission check
Vector<String> userPermissions = getUserPermissions();
Vector<String> requiredPermissions = getRequiredPermissions();

if (!userPermissions.containsAll(requiredPermissions)) {
    throw new SecurityException("The user lacks the necessary permissions");
}

Frequently Asked Questions

Q1: Does the containsAll() method modify the original Vector?

A: No, containsAll() is only a query method and does not modify the original Vector.

Q2: If the Vector contains duplicate elements, how does containsAll() work?

A: containsAll() only cares whether elements exist, not how many times they appear. As long as each element appears at least once, it returns true.

Q3: Can collections of different types be checked?

A: Yes, as long as the element types are compatible (comparable via the equals() method).


Summary

Vector.containsAll()The method is a practical collection operation utility that can conveniently check whether one collection is a subset of another. Understanding and correctly using this method can help developers write cleaner and more efficient collection operation code.

Java Vector Java Vector

Other Extensions