Dart Set removeAll()
Syntax & Examples
Syntax of Set.removeAll()
The syntax of Set.removeAll() method is:
void removeAll(Iterable<Object> elements) This removeAll() method of Set removes each element of elements from this set.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
elements | required | An Iterable containing elements to be removed from the Set. |
Return Type
Set.removeAll() returns value of type void.
✐ Examples
1 Removing elements from Set of numbers
In this example,
- We create a Set
numSetwith values {1, 2, 3, 4}. - We create a List
elementsToRemovecontaining elements [2, 3] to be removed fromnumSet. - We use the
removeAll()method to remove all elements fromnumSetthat are inelementsToRemove. - We print the updated
numSetafter removal.
Dart Program
void main() {
Set<int> numSet = {1, 2, 3, 4};
List<int> elementsToRemove = [2, 3];
numSet.removeAll(elementsToRemove);
print(numSet);
}Output
{1, 4}2 Removing elements from Set of characters
In this example,
- We create a Set
charSetwith values {'a', 'b', 'c', 'd'}. - We create a List
elementsToRemovecontaining elements ['b', 'd'] to be removed fromcharSet. - We use the
removeAll()method to remove all elements fromcharSetthat are inelementsToRemove. - We print the updated
charSetafter removal.
Dart Program
void main() {
Set<String> charSet = {'a', 'b', 'c', 'd'};
List<String> elementsToRemove = ['b', 'd'];
charSet.removeAll(elementsToRemove);
print(charSet);
}Output
{a, c}3 Removing elements from Set of strings
In this example,
- We create a Set
wordSetwith values {'apple', 'banana', 'cherry', 'orange'}. - We create a List
elementsToRemovecontaining elements ['banana', 'orange'] to be removed fromwordSet. - We use the
removeAll()method to remove all elements fromwordSetthat are inelementsToRemove. - We print the updated
wordSetafter removal.
Dart Program
void main() {
Set<String> wordSet = {'apple', 'banana', 'cherry', 'orange'};
List<String> elementsToRemove = ['banana', 'orange'];
wordSet.removeAll(elementsToRemove);
print(wordSet);
}Output
{apple, cherry}Summary
In this Dart tutorial, we learned about removeAll() method of Set: the syntax and few working examples with output and detailed explanation for each example.