DEV Community

Cover image for Exploring Dart Collection Types: Part 2 — Sets
Motabar Javaid
Motabar Javaid

Posted on

Exploring Dart Collection Types: Part 2 — Sets

In the previous article, We discussed Lists in detail. Today we’ll discuss the second type of Dart’s Collection i-e; Sets.

A Set in dart is an unordered collection of unique objects. Since the collection is of unordered objects, we cannot iterate through the elements using Indices like we can in other Collection Types such as Lists.

One use case of choosing Sets over Lists is that suppose we want to have a collection of Unique names of Employees and the order of names does not really matter. Sets could work out better in this scenario than List as Sets cannot have duplicates.

Let’s get our hands dirty and see how Set is declared and what does it has to offer:

A Set can be defined using Set Constructor as well as defining it explicitly as:


void main() {
  // Defining a set using Constructor
  var thisIsASet = Set();

  // Implicitly declaring a Set of dynamic types
  var setOfNames = {'John', 'Emilia', 'Robb', 'Jack', 'Jorah', 'Stark', 1, 4};

  //Explicitly defining a set of just String values
  Set<String> colors = {'Blue', "Red", "Orange", "Green"};


}

Enter fullscreen mode Exit fullscreen mode

We can also convert a List into a set using Set.from() and providing the List as an argument to it as:


void main()
{

  List listOfNumbers = [1,2,3,4,5,5];

 //converting a List to a set
 Set setOfNumbers = Set.from(listOfNumbers);

 //Returns set having no duplicates
 print(setOfNumbers); //returns {1,2,3,4,5}

}
Enter fullscreen mode Exit fullscreen mode

Since a Set’s element does not have an order, we cannot access it using Index but we can access it using elementAt() method that is available for sets. The method takes in an integer value as an index and returns the element which is present at that index as:


void main() {

  List listOfNumbers = [1, 2, 3, 4, 5, 5, 6];

  // converting a list into a Set
  Set setOfNumbers = Set.from(listOfNumbers);

  //Returns set having no duplicates
  print(setOfNumbers); //returns {1,2,3,4,5,6}


}

Enter fullscreen mode Exit fullscreen mode

Dart’s Set offers general Set Operations such as Union, Intersection, difference, Compliment etc. Let’s go through them one by one:

union():

The union() method in takes in a set as a parameter and unites the elemtents with the Set on which it is called on. Let’s make it less complex as see it in action:


void main() {
  Set<int> setOfOddNumbers = {1, 3, 5, 7, 9};

  Set<int> setOfEvenNumbers = {2, 4, 6, 8, 10};

  // Taking union of set of odd and even numbers:
  print(
      "${setOfOddNumbers.union(setOfEvenNumbers)}"); //rturns {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}

}
Enter fullscreen mode Exit fullscreen mode

intersection():

The intersection() method, just like the union method takes in a set as a parameter and intersects it with the set on which it is called on. In code, it works as follows:


void main() {
  Set<int> firstSet = {1, 2, 3, 4, 5, 6};

  Set<int> secondSet = {1, 4, 2, 6, 8, 7};

  // Taking intersection of set of first and second set:
  print("${firstSet.intersection(secondSet)}"); //returns {1,2,4,6} as only these are present in first and the second set

}
Enter fullscreen mode Exit fullscreen mode

difference():

The differnce() method returns back the set having the elements that are in the first Set but not in the second. It works by taking in a set as a parameter and taking difference with the set on which it is called on as follows:

void main() {
  Set<int> firstSet = {1, 2, 3, 4, 5, 6};

  Set<int> secondSet = {1, 4, 2, 6, 8, 7};

  // Taking the difference of first and second set:
  print("${firstSet.difference(secondSet)}"); //returns {3,5} as only these two are present in first but not in the second set

}

Enter fullscreen mode Exit fullscreen mode

Compliment of a set is just like the difference between the sets. The only difference is the first set is a Universal Set (A set containing elements of all the sets).

In addition to basic Set Operations, Dart offer plenty of other methods for its Set Collection Type. Let’s go through them one by one and see what they do.

add():

Like the name suggests, the add method is used to add elements into the set. The method takes in an element as parameter and adds it up into the set on which it is called on as follows:


void main() {
  Set<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8};

  // Adding elements into the set
  numbers.add(9);
  numbers.add(10);
  // Printing the modified set
  print(numbers); //returns {1,2,3,4,5,6,7,8,9,10}
}

Enter fullscreen mode Exit fullscreen mode

remove():

remove() method on Sets works just like the add method but does the opposite. It removes the elements from the list. The method takes in an element as the parameter that we want to remove from the set and removes it. Let’s see that in code:


void main() {
  Set<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  // removing elements from the set
  numbers.remove(4);
  numbers.remove(6);
  // Printing the modified set
  print(numbers); //returns {1,2,3,5,7,8,9,10}
}


Enter fullscreen mode Exit fullscreen mode

clear():

Sometimes we want to delete all the elements that are present in the Set. For that purpose, the Set clear() method comes in handy. Just call it on the set you want to clear and it will remove all the elements present in it.


void main() {
  Set<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  // Deletes all the elements present in the set
  numbers.clear();

  print(numbers); //returns {}
}

Enter fullscreen mode Exit fullscreen mode

contains():

The contain() method on Set helps us if we want to check if a certain element is present in the list. It takes in the element we want to check as a parameter and returns true if the element is in the list and false if it is not. Let’s see that in code:


void main()
{
  Set<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8};

  // Check if an element exists in the list
  print(numbers.contains(2)); //returns true as 2 is present in the set

}


Enter fullscreen mode Exit fullscreen mode

These are some of the methods that are more repeatitively used with Sets. If you’re interested in learning more about them, you can check them out at dart.dev.

That’s all for now Folks! Thanks for reading this article ❤️ Will soon be posting the next parts. Stay tuned!

Feel free to post any queries or corrections you think are required ✔

Do leave a feedback so I can improve on my content. Thankyou! 😃

If you’re interested, here are some of my other articles on

Discussion (0)