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

Java Vector containsAll() Method

Java Vector containsAll() Method

Java Vector Java Vector

containsAll() The method is in Java's Vector A collection operation method provided by the class, used to check whether the current Vector contains all elements in the specified collection. It is inherited from the Collection interface and implemented in the Vector class.

Syntax format


public boolean containsAll(Collection c)

Method parameters

Parameters Type Description
c Collection The collection of elements to check whether they are contained in the current Vector

Return Value

This method returns a boolean value:


Method characteristics

  1. Full Inclusion Check: Returns true only when the Vector contains all elements in the collection
  2. Empty Collection Handling: If the passed-in collection is empty, the method returns true
  3. Order Independent: Does not care about the order of elements in the collection
  4. Duplicate Element Handling: Considers the number of duplicate 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-Inclusion

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

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


Notes

  1. Performance considerations: For large collections, this method may require O(n) time complexity
  2. : If the list is modified during iteration, it may throw:
  3. If the passed-in collection argument is null, a NullPointerException is thrown
  4. Vector can contain null elements, and null elements are also considered during the check
  5. - If the passed-in collection argument is null, a NullPointerException is thrown - Vector can contain null elements, and null elements are also considered during the check
    1. Equality Check: Uses the element's equals() method to determine inclusion

    Comparison with Related Methods

    Method Description Differences from containsAll()
    contains() Checks whether a single element exists Checks only 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 check: Verify whether a user has all required permissions
    2. Shopping Cart: Check whether the inventory contains all items in the shopping cart
    3. Course Enrollment: Verify whether a student has completed all required courses

    Example

    // Practical 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 just a query method and does not modify the original Vector.

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

    A: containsAll() only cares whether the elements exist, not the number of occurrences. It returns true as long as each element appears at least once.

    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 tool that conveniently checks whether one collection is a subset of another. Understanding and correctly using this method can help developers write more concise and efficient collection operation code.

    Java Vector Java Vector