Exploring Dart Collection Types: Part 2 — Sets

Motabar Javaid
5 min readSep 5, 2021

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:

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

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:

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:

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:

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:

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:

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:

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.

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:

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!

Clap 👏 If this article helped you.

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:

--

--

Motabar Javaid

I try to teach what I learn. Flutter Developer | Dart Enthusiast