In Dart, a Set is an unordered collection of unique items. Sets are particularly useful when you need to ensure that no duplicate elements are present in your collection. In this section, we will cover the following topics:
- Introduction to Sets
- Creating Sets
- Adding and Removing Elements
- Set Operations
- Iterating Over Sets
- Practical Examples
- Exercises
- Introduction to Sets
A Set in Dart is a collection of unique elements. Unlike Lists, Sets do not maintain the order of elements. Sets are useful when you need to store a collection of items and ensure that each item is unique.
- Creating Sets
You can create a Set in Dart using the Set
class. Here are a few ways to create a Set:
Using the Set Constructor
Using Set Literals
void main() { Set<String> fruits = {'apple', 'banana', 'orange'}; print(fruits); // Output: {apple, banana, orange} }
Using the Set.from
Constructor
void main() { List<int> list = [1, 2, 3, 4, 4, 5]; Set<int> uniqueNumbers = Set.from(list); print(uniqueNumbers); // Output: {1, 2, 3, 4, 5} }
- Adding and Removing Elements
You can add and remove elements from a Set using the add
, addAll
, remove
, and removeAll
methods.
Adding Elements
void main() { Set<String> fruits = {'apple', 'banana'}; fruits.add('orange'); print(fruits); // Output: {apple, banana, orange} }
Adding Multiple Elements
void main() { Set<String> fruits = {'apple', 'banana'}; fruits.addAll({'orange', 'grape'}); print(fruits); // Output: {apple, banana, orange, grape} }
Removing Elements
void main() { Set<String> fruits = {'apple', 'banana', 'orange'}; fruits.remove('banana'); print(fruits); // Output: {apple, orange} }
Removing Multiple Elements
void main() { Set<String> fruits = {'apple', 'banana', 'orange', 'grape'}; fruits.removeAll({'banana', 'grape'}); print(fruits); // Output: {apple, orange} }
- Set Operations
Dart provides several methods to perform operations on Sets, such as union, intersection, and difference.
Union
The union
method returns a new Set containing all elements from both Sets.
void main() { Set<int> setA = {1, 2, 3}; Set<int> setB = {3, 4, 5}; Set<int> unionSet = setA.union(setB); print(unionSet); // Output: {1, 2, 3, 4, 5} }
Intersection
The intersection
method returns a new Set containing only the elements that are present in both Sets.
void main() { Set<int> setA = {1, 2, 3}; Set<int> setB = {3, 4, 5}; Set<int> intersectionSet = setA.intersection(setB); print(intersectionSet); // Output: {3} }
Difference
The difference
method returns a new Set containing the elements that are present in the first Set but not in the second Set.
void main() { Set<int> setA = {1, 2, 3}; Set<int> setB = {3, 4, 5}; Set<int> differenceSet = setA.difference(setB); print(differenceSet); // Output: {1, 2} }
- Iterating Over Sets
You can iterate over the elements of a Set using a for
loop or the forEach
method.
Using a for
Loop
void main() { Set<String> fruits = {'apple', 'banana', 'orange'}; for (String fruit in fruits) { print(fruit); } }
Using the forEach
Method
void main() { Set<String> fruits = {'apple', 'banana', 'orange'}; fruits.forEach((fruit) => print(fruit)); }
- Practical Examples
Example 1: Removing Duplicates from a List
void main() { List<int> numbers = [1, 2, 2, 3, 4, 4, 5]; Set<int> uniqueNumbers = Set.from(numbers); print(uniqueNumbers); // Output: {1, 2, 3, 4, 5} }
Example 2: Finding Common Elements Between Two Lists
void main() { List<int> listA = [1, 2, 3, 4]; List<int> listB = [3, 4, 5, 6]; Set<int> setA = Set.from(listA); Set<int> setB = Set.from(listB); Set<int> commonElements = setA.intersection(setB); print(commonElements); // Output: {3, 4} }
- Exercises
Exercise 1: Create a Set of Integers
Create a Set of integers and add the numbers 1 to 5 to it. Then, print the Set.
Solution:
Exercise 2: Perform Set Operations
Given two Sets of integers, perform union, intersection, and difference operations and print the results.
Solution:
void main() { Set<int> setA = {1, 2, 3}; Set<int> setB = {3, 4, 5}; Set<int> unionSet = setA.union(setB); Set<int> intersectionSet = setA.intersection(setB); Set<int> differenceSet = setA.difference(setB); print('Union: $unionSet'); // Output: Union: {1, 2, 3, 4, 5} print('Intersection: $intersectionSet'); // Output: Intersection: {3} print('Difference: $differenceSet'); // Output: Difference: {1, 2} }
Exercise 3: Remove Duplicates from a List
Given a List of integers with duplicate values, create a Set to remove the duplicates and print the unique values.
Solution:
void main() { List<int> numbers = [1, 2, 2, 3, 4, 4, 5]; Set<int> uniqueNumbers = Set.from(numbers); print(uniqueNumbers); // Output: {1, 2, 3, 4, 5} }
Conclusion
In this section, we have learned about Sets in Dart, including how to create them, add and remove elements, perform set operations, and iterate over them. We also covered practical examples and exercises to reinforce the concepts. Understanding Sets is crucial for managing collections of unique items efficiently in your Dart programs. In the next section, we will explore Maps, another essential collection type in Dart.
Dart Programming Course
Module 1: Introduction to Dart
- Introduction to Dart
- Setting Up the Development Environment
- Your First Dart Program
- Basic Syntax and Structure