Dart Set


In Dart, a set is an unordered collection of unique elements. Sets are useful when you need to ensure that no duplicate values exist in your collection. Dart provides a built-in Set class that offers various methods and properties for creating and managing sets. Below is an overview of sets in Dart, including how to create, manipulate, and utilize them effectively.

Creating Sets

You can create sets in Dart using the Set class. Sets can be created using set literals or the Set constructor.

  1. Using Set Literals:
void main() { // Create a set of integers Set<int> numbers = {1, 2, 3, 4, 5}; // Create a set of strings Set<String> fruits = {'apple', 'banana', 'cherry'}; print(numbers); // Output: {1, 2, 3, 4, 5} print(fruits); // Output: {apple, banana, cherry} }
  1. Using the Set Constructor:

You can also create an empty set using the Set constructor.

void main() { // Create an empty set Set<String> emptySet = Set<String>(); // Create a set from a list Set<int> fromList = Set.from([1, 2, 3, 4]); print(emptySet); // Output: {} print(fromList); // Output: {1, 2, 3, 4} }

Accessing and Modifying Sets

Since sets are unordered, you cannot access elements by index. However, you can check for the existence of an element or perform operations such as adding or removing elements.

  • Adding Elements: Use the add() or addAll() methods.
void main() { Set<String> fruits = {'apple', 'banana'}; // Add a single element fruits.add('cherry'); // Add 'cherry' // Add multiple elements fruits.addAll({'date', 'elderberry'}); print(fruits); // Output: {apple, banana, cherry, date, elderberry} }
  • Removing Elements: Use the remove() or clear() methods.
void main() { Set<String> fruits = {'apple', 'banana', 'cherry'}; // Remove an element fruits.remove('banana'); // Clear the set fruits.clear(); print(fruits); // Output: {} }

Set Properties and Methods

Dart provides various properties and methods to work with sets:

  • Length: Get the number of elements in the set.
void main() { Set<String> fruits = {'apple', 'banana', 'cherry'}; print(fruits.length); // Output: 3 }
  • Contains: Check if an element exists in the set.
void main() { Set<String> fruits = {'apple', 'banana', 'cherry'}; print(fruits.contains('banana')); // Output: true print(fruits.contains('date')); // Output: false }
  • Intersection: Find common elements between two sets.
void main() { Set<int> setA = {1, 2, 3, 4}; Set<int> setB = {3, 4, 5, 6}; Set<int> intersection = setA.intersection(setB); print(intersection); // Output: {3, 4} }
  • Union: Combine two sets into one.
void main() { Set<int> setA = {1, 2, 3}; Set<int> setB = {3, 4, 5}; Set<int> union = setA.union(setB); print(union); // Output: {1, 2, 3, 4, 5} }
  • Difference: Find elements in one set that are not in another.
void main() { Set<int> setA = {1, 2, 3}; Set<int> setB = {3, 4, 5}; Set<int> difference = setA.difference(setB); print(difference); // Output: {1, 2} }

Iterating Over Sets

You can iterate over the elements of a set using a for loop or a forEach method.

  1. Using a for Loop:
void main() { Set<String> fruits = {'apple', 'banana', 'cherry'}; for (var fruit in fruits) { print(fruit); } }
  1. Using the forEach Method:
void main() { Set<String> fruits = {'apple', 'banana', 'cherry'}; fruits.forEach((fruit) { print(fruit); }); }

Conclusion

Sets in Dart are a powerful data structure for managing collections of unique elements. Their unordered nature and built-in methods for performing set operations make them ideal for scenarios where duplicates should be avoided or when performing mathematical set operations. Understanding how to effectively work with sets will enable you to write cleaner and more efficient Dart code, whether you're managing data collections, performing calculations, or implementing algorithms that require unique values. By mastering sets, you can enhance the functionality and performance of your Dart applications.