Java ArrayList containsAll() Method
The containsAll() method is used to check whether the arraylist contains all elements of a specified collection.
The syntax of the containsAll() method is:
arraylist.containsAll(Collection c);
**Note:** arraylist is an object of the ArrayList class.
Parameter Description:
- collection - the collection parameter.
Return Value
If the dynamic array contains all elements of the collection, returns true.
If the elements present in the arraylist are incompatible with the elements in the specified collection, a ClassCastException is thrown.
If the collection contains a null element and the arraylist does not allow null values, a NullPointerException is thrown.
**Note:** It can be understood this way: the containsAll() method checks whether the collection is a subset of the dynamic array.
Example
The following example demonstrates the use of containsAll:
Example
import
java.util.ArrayList
;
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
// Create a dynamic array
ArrayList
<
String
sites
=
new
ArrayList
<>
(
);
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
System
.
out
.
println
(
"ArrayList 1: "
sites
)
;
// Create another dynamic array
ArrayList
<
String
sites2
=
new
ArrayList
<>
(
);
// Add elements to the dynamic array
sites2.
add
(
"Runoob"
)
;
sites2.
add
(
"Google"
)
;
System
.
out
.
println
(
"ArrayList 2: "
sites2
)
;
// Check whether dynamic array 1 contains dynamic array 2
boolean
result1
=
sites.
containsAll
(
sites2
)
;
System
.
out
.
println
(
"ArrayList 1 contains all elements of ArrayList 2: "
result1
)
;
// Check whether array 2 contains array 1
boolean
result2
=
sites2.
containsAll
(
sites
)
;
System
.
out
.
println
(
"ArrayList 2 contains all elements of ArrayList 1: "
result2
)
;
}
}
The output of the above program is:
ArrayList 1: [Google, Runoob, Taobao]
ArrayList 2: [Runoob, Google]
ArrayList 1 包含了 ArrayList 2 的所有元素: true
ArrayList 2 包含了 ArrayList 1 的所有元素: false
In the example above, we created two dynamic arrays named sites and sites2.
Note these lines:
// 返回 true
sites.containsAll(sites2);
// returns false
sites2.containsAll(sites)
The containsAll() method checks whether sites contains all elements of sites2; the elements are contained, so it returns true.
The containsAll() method checks whether sites2 contains all elements of sites; the elements are not contained, so it returns false.
** containsAll() Method Between Java ArrayList and HashSet **
Example
import
java.util.ArrayList
;
import
java.util.HashSet
;
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
// Create an array
ArrayList
<
Integer
numbers
=
new
ArrayList
<>
(
);
// Insert elements into the array
numbers.
add
(
1
)
;
numbers.
add
(
2
)
;
numbers.
add
(
3
)
;
System
.
out
.
println
(
"ArrayList: "
numbers
)
;
// Create a HashSet
HashSet
<
Integer
primeNumbers
=
new
HashSet
<>
(
);
// Add elements to the HashSet
primeNumbers.
add
(
2
)
;
primeNumbers.
add
(
3
)
;
System
.
out
.
println
(
"HashSet: "
primeNumbers
)
;
// Check whether the ArrayList contains all elements of the HashSet
boolean
result1
=
numbers.
containsAll
(
primeNumbers
)
;
System
.
out
.
println
(
"Does ArrayList contain all elements of HashSet: "
result1
)
;
// Check whether the HashSet contains all elements of the ArrayList
boolean
result2
=
primeNumbers.
containsAll
(
numbers
)
;
System
.
out
.
println
(
"Does HashSet contain all elements of ArrayList: "
result2
)
;
}
}
The output of the above program is:
ArrayList: [1, 2, 3]
HashSet: [2, 3]
ArrayList 是否 HashSet 中的所有元素: true
HashSet 是否 ArrayList 中的所有元素: false
In the example above, we created an ArrayList named numbers and a HashSet named primeNumbers.
Java ArrayList