Dart Set containsAll()
Syntax & Examples
Set.containsAll() method
The `containsAll` method in Dart returns whether this Set contains all the elements of another iterable.
Syntax of Set.containsAll()
The syntax of Set.containsAll() method is:
bool containsAll(Iterable<Object> other)
This containsAll() method of Set returns whether this Set contains all the elements of other.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
other | required | An iterable of objects to check for containment in this Set. |
Return Type
Set.containsAll() returns value of type bool
.
✐ Examples
1 Check for numbers
In this example,
- We create a Set
set
containing numbers from 1 to 5. - We create a List
numbers
containing elements 2 and 4. - We use the
containsAll()
method to check if the set contains all numbers from the list. - We print the result to standard output.
Dart Program
void main() {
Set<int> set = {1, 2, 3, 4, 5};
List<int> numbers = [2, 4];
bool result = set.containsAll(numbers);
print('Set contains all numbers: $result');
}
Output
Set contains all numbers: true
2 Check for characters
In this example,
- We create a Set
set
containing characters 'a' to 'e'. - We create a List
chars
containing characters 'a', 'b', and 'f'. - We use the
containsAll()
method to check if the set contains all characters from the list. - We print the result to standard output.
Dart Program
void main() {
Set<String> set = {'a', 'b', 'c', 'd', 'e'};
List<String> chars = ['a', 'b', 'f'];
bool result = set.containsAll(chars);
print('Set contains all characters: $result');
}
Output
Set contains all characters: false
3 Check for strings
In this example,
- We create a Set
set
containing strings 'apple', 'banana', 'orange', and 'grape'. - We create a List
fruits
containing strings 'banana', 'orange', and 'kiwi'. - We use the
containsAll()
method to check if the set contains all strings from the list. - We print the result to standard output.
Dart Program
void main() {
Set<String> set = {'apple', 'banana', 'orange', 'grape'};
List<String> fruits = ['banana', 'orange', 'kiwi'];
bool result = set.containsAll(fruits);
print('Set contains all fruits: $result');
}
Output
Set contains all fruits: false
Summary
In this Dart tutorial, we learned about containsAll() method of Set: the syntax and few working examples with output and detailed explanation for each example.