Java Vector containsAll() Method
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:
true: If the current Vector contains all elements in the specified collectionfalse: If the current Vector does not contain all elements in the specified collection
Method characteristics
- Full Inclusion Check: Returns true only when the Vector contains all elements in the collection
- Empty Collection Handling: If the passed-in collection is empty, the method returns true
- Order Independent: Does not care about the order of elements in the collection
- 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
- Performance considerations: For large collections, this method may require O(n) time complexity
- : If the list is modified during iteration, it may throw:
- 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
- Permission check: Verify whether a user has all required permissions
- Shopping Cart: Check whether the inventory contains all items in the shopping cart
- 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