Dart Sets
Dart Sets
A set is a collection of items where every item is unique.
A set can never hold the same value twice.
You write a set with curly brackets {}, and separate the items with commas:
Example
void main() {
var fruits = {'apple', 'banana', 'cherry'};
print(fruits);
}
Try it Yourself »
Result:
{apple, banana, cherry}
Dart sees that all the items are strings, so the type of fruits is Set<String>.
You can also write the type yourself:
Example
void main() {
Set<int> scores = {10, 20, 30};
print(scores);
print(scores.length);
}
Try it Yourself »
Result:
{10, 20, 30}
3
The length property tells you how many items the set has.
No Duplicates
If you put the same value in a set more than once, the set keeps only one copy:
Example
void main() {
var colors = {'red', 'green', 'red', 'blue', 'green'};
print(colors);
print(colors.length);
}
Try it Yourself »
Result:
{red, green, blue}
3
This makes a set useful when you want to remove duplicates from a list.
The toSet() method turns a list into a set:
Example
void main() {
var answers = ['yes', 'no', 'yes', 'yes', 'no'];
var unique = answers.toSet();
print(unique);
}
Try it Yourself »
Result:
{yes, no}
Empty Set or Empty Map?
Both sets and maps use curly brackets.
Because of this, an empty {} is not a set.
Dart reads an empty {} as an empty map:
Example
void main() {
var things = {};
print(things is Map);
print(things is Set);
}
Try it Yourself »
Result:
true
false
To create an empty set, put the item type in angle brackets before the curly brackets, or write the type on the variable:
Example
void main() {
var cars = <String>{};
Set<String> bikes = {};
print(cars is Set);
print(bikes is Set);
}
Try it Yourself »
Result:
true
true
Add, Remove and Check Items
Use add() to add an item, and remove() to remove an item.
Use contains() to check if an item is in the set.
Example
void main() {
var cars = {'Volvo', 'BMW'};
cars.add('Ford');
cars.remove('BMW');
print(cars);
print(cars.contains('Volvo'));
print(cars.contains('BMW'));
}
Try it Yourself »
Result:
{Volvo, Ford}
true
false
The add() method returns true if the item was added.
It returns false if the item was already in the set:
Example
void main() {
var cars = {'Volvo', 'BMW'};
print(cars.add('Ford'));
print(cars.add('Volvo'));
print(cars);
}
Try it Yourself »
Result:
true
false
{Volvo, BMW, Ford}
Note: A set has no index, so you cannot write cars[0] like you do with a list.
Use a set when you only need to know if an item is there, and a list when the order and the position matter.
Union, Intersection and Difference
You can combine two sets in three ways:
union()returns all items from both setsintersection()returns only the items that are in both setsdifference()returns the items in the first set that are not in the second set
Example
void main() {
var tom = {'Math', 'English', 'Art'};
var anna = {'Math', 'Music', 'Art'};
print(tom.union(anna));
print(tom.intersection(anna));
print(tom.difference(anna));
}
Try it Yourself »
Result:
{Math, English, Art, Music}
{Math, Art}
{English}
These methods return a new set.
The original sets do not change.