Dart Sets
Working with unique collections in Dart
🎯 Understanding Dart Sets
Sets are collections of unique elements with no duplicates allowed. They're perfect for storing distinct values and performing mathematical set operations like union and intersection.
// Creating a set with unique elements
Set<String> colors = {'red', 'blue', 'green', 'red'};
print(colors); // {red, blue, green} - duplicate removed
Set Features
Unique Elements
Automatically removes duplicates
{1, 2, 2, 3} // Results in {1, 2, 3}
Fast Lookup
Efficient contains() operations
set.contains('value') // O(1) average
Set Operations
Union, intersection, difference
set1.union(set2)
set1.intersection(set2)
Unordered
No guaranteed element order
{'c', 'a', 'b'} // Order may vary
🔹 Creating Sets
Different ways to create and initialize sets:
void main() {
// Empty set
Set<String> emptySet = {};
Set<int> emptyNumbers = <int>{};
// Set with initial values
Set<String> fruits = {'apple', 'banana', 'orange'};
var numbers = {1, 2, 3, 4, 5};
// From list (removes duplicates)
List<int> listWithDuplicates = [1, 2, 2, 3, 3, 4];
Set<int> uniqueNumbers = listWithDuplicates.toSet();
// Using Set constructor
Set<String> colors = Set.from(['red', 'blue', 'red', 'green']);
print('Fruits: $fruits');
print('Unique numbers: $uniqueNumbers');
print('Colors: $colors');
}
Output:
Fruits: {apple, banana, orange}
Unique numbers: {1, 2, 3, 4}
Colors: {red, blue, green}
🔹 Set Operations
Basic operations for adding, removing, and checking elements:
void main() {
Set<String> animals = {'cat', 'dog', 'bird'};
// Adding elements
animals.add('fish');
animals.addAll(['rabbit', 'hamster']);
print('After adding: $animals');
// Removing elements
animals.remove('hamster');
print('After removing hamster: $animals');
// Checking elements
print('Contains cat: ${animals.contains('cat')}');
print('Is empty: ${animals.isEmpty}');
print('Length: ${animals.length}');
// Try adding duplicate
bool added = animals.add('cat'); // Returns false
print('Added duplicate cat: $added');
print('Final set: $animals');
}
Output:
After adding: {cat, dog, bird, fish, rabbit, hamster}
After removing hamster: {cat, dog, bird, fish, rabbit}
Contains cat: true
Is empty: false
Length: 5
Added duplicate cat: false
Final set: {cat, dog, bird, fish, rabbit}
🔹 Mathematical Set Operations
Perform mathematical operations between sets:
void main() {
Set<int> setA = {1, 2, 3, 4, 5};
Set<int> setB = {4, 5, 6, 7, 8};
// Union - all elements from both sets
Set<int> union = setA.union(setB);
print('Union: $union');
// Intersection - common elements
Set<int> intersection = setA.intersection(setB);
print('Intersection: $intersection');
// Difference - elements in A but not in B
Set<int> difference = setA.difference(setB);
print('Difference (A - B): $difference');
// Check relationships
Set<int> subset = {2, 3};
print('Is {2, 3} subset of A: ${subset.difference(setA).isEmpty}');
}
Output:
Union: {1, 2, 3, 4, 5, 6, 7, 8}
Intersection: {4, 5}
Difference (A - B): {1, 2, 3}
Is {2, 3} subset of A: true
🔹 Iterating Through Sets
Different ways to loop through set elements:
void main() {
Set<String> languages = {'Dart', 'Java', 'Python', 'JavaScript'};
// For-in loop
print('Programming languages:');
for (String language in languages) {
print('- $language');
}
// forEach method
print('\nUsing forEach:');
languages.forEach((language) {
print('* $language');
});
// Convert to list for indexed access
List<String> languageList = languages.toList();
print('\nWith index:');
for (int i = 0; i < languageList.length; i++) {
print('${i + 1}. ${languageList[i]}');
}
}
Output:
Programming languages:
- Dart
- Java
- Python
- JavaScript
Using forEach:
* Dart
* Java
* Python
* JavaScript
With index:
1. Dart
2. Java
3. Python
4. JavaScript