To check if a set contains all the items of another set in Dart, you can use the containsAll
method of the Set
class.
In this example,
setA
and setB
and initialize them with some integer elements.containsAll
method of setA
to check if all elements in setB
are also present in setA
.containsAll
method returns a boolean value: true
if setA
contains all elements of setB
, and false
otherwise.setA
contains all elements of setB
.void main() {
Set<int> setA = {1, 2, 3, 4, 5};
Set<int> setB = {2, 3};
bool containsAll = setA.containsAll(setB);
print('Does setA contain all elements of setB? $containsAll');
}
Does setA contain all elements of setB? true
In this example,
setA
and setB
and initialize them with some string elements.containsAll
method of setA
to check if all elements in setB
are also present in setA
.containsAll
method returns a boolean value: true
if setA
contains all elements of setB
, and false
otherwise.setA
contains all elements of setB
.void main() {
Set<String> setA = {'apple', 'banana', 'cherry'};
Set<String> setB = {'banana', 'cherry'};
bool containsAll = setA.containsAll(setB);
print('Does setA contain all elements of setB? $containsAll');
}
Does setA contain all elements of setB? true
In this tutorial, we learned How to check if a Set contains all the items of Another Set in Dart language with well detailed examples.