Dart Set Common functions


In Dart, a Set is an unordered collection of unique items. It is particularly useful when you want to store a collection of elements without duplicates. Dart provides various built-in methods to manipulate sets effectively. Here’s an overview of common functions you can use with sets in Dart:

1. Creation and Initialization

  • Creating a Set:
    Set<int> numbers = {}; // An empty set Set<String> fruits = {'Apple', 'Banana', 'Orange'}; // A set with initial elements

2. Adding Elements

  • add(): Adds an element to the set.

    numbers.add(1); // numbers: {1}
  • addAll(): Adds multiple elements to the set.

    numbers.addAll([2, 3, 4]); // numbers: {1, 2, 3, 4}

3. Removing Elements

  • remove(): Removes a specified element from the set.

    numbers.remove(2); // numbers: {1, 3, 4}
  • removeAll(): Removes multiple elements from the set.

    numbers.removeAll({3, 4}); // numbers: {1}
  • removeWhere(): Removes elements that satisfy a given condition.

    numbers.removeWhere((n) => n > 1); // numbers: {1}
  • clear(): Removes all elements from the set.

    numbers.clear(); // numbers: {}

4. Checking Existence

  • contains(): Checks if the set contains a specific element.

    bool hasOne = numbers.contains(1); // hasOne: false (if set is empty)
  • isEmpty: Checks if the set is empty.

    bool emptyCheck = numbers.isEmpty; // emptyCheck: true
  • isNotEmpty: Checks if the set is not empty.

    bool notEmptyCheck = numbers.isNotEmpty; // notEmptyCheck: false

5. Iterating Through the Set

  • forEach(): Executes a function for each element in the set.
    fruits.forEach((fruit) { print(fruit); // Outputs each fruit in the set });

6. Set Operations

  • union(): Combines two sets and returns a new set with all unique elements.

    Set<int> otherNumbers = {3, 4, 5}; Set<int> unionSet = numbers.union(otherNumbers); // unionSet: {1, 3, 4, 5}
  • intersection(): Returns a new set containing elements present in both sets.

    Set<int> intersectionSet = numbers.intersection(otherNumbers); // intersectionSet: {3}
  • difference(): Returns a new set with elements in the first set that are not in the second.

    Set<int> differenceSet = numbers.difference(otherNumbers); // differenceSet: {1}
  • containsAll(): Checks if the set contains all elements of another collection.

    bool allExist = numbers.containsAll({1}); // allExist: true

7. Set Properties

  • length: Returns the number of elements in the set.
    int length = numbers.length; // length: 0 (if set is empty)

8. Conversion

  • toList(): Converts the set to a list.

    List<int> numberList = numbers.toList(); // numberList: []
  • toSet(): Converts a list or another collection to a set.

    Set<int> newSet = {1, 2, 3}.toSet(); // newSet: {1, 2, 3}

Conclusion

Dart's Set collection provides a robust set of functions that allow you to create, manipulate, and access unordered collections of unique elements efficiently. Understanding these common functions helps you manage data in your applications effectively, especially when you need to ensure that there are no duplicates. Whether you're adding or removing elements, performing set operations, or checking for existence, Dart's set methods simplify these tasks and enhance your coding experience.