Programming in Dart: Control Flow & Collections

May 3 2022 · Dart 2.15, DartPad, DartPad

Part 2: More Collections

15. Create Sets

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 14. Challenge: Work with Maps Next episode: 16. Work with Sets

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 15. Create Sets

So far, you’ve learned about two collection types in Dart. First you learned about a list where you can hold an ordered list of items. Then you learned about the map that allows you lookup values based on a key. Now, for the final fundamental collection type - the set.

A set is an unordered collection of unique values of the same type. For example, here’s a set of Flutter butterflies doing different activities.

What do I mean by “a collection of unique values”, though? Well, we can insert a Flutter butterfly holding a Ray Wenderlich into this set and that would work! We don’t have this image in our collection yet.

But if we tried to insert another copy of the Flutter butterfly reading some books. Well, nothing would happen. That images is already in the collection, and sets can only store unique values.

Another way to say that is “sets don’t store duplicate values”. This can be extremely useful when you want to ensure that an item doesn’t appear more than once in your collection. You can also use sets when the order of your items isn’t important.

To get started, open up a new instance over at dartpad.dev. Now lets create a set. Like maps and lists, there are a couple of ways to make them. First, let’s define a set that will hold integers.

var someSet = Set<int>();

You’ll notice that DartPad gives us a warning. It tells us to use a collection literal whenever possible. Let’s take that suggestion an update our set.

var someSet = <int>{};

Here we use the braces which looks just like a map, but Dart is smart enough to know we are using a Set. We can also initialize the set with some values.

var someSet = { 1, 2, 3, 4, 1 };

Notice that we didn’t need to specify the type. But remember not too long ago, I said that sets can’t hold duplicate values. Yet, I passed in two duplicates. In fact, Dart actually warns me that I’m passing duplicates. Let’s print out the set.

print(someSet);

Run the program. You’ll see that the duplicate it automatically dropped. There are only four values contained in the set.

To find out if a set already contains a certain value, you can use the contains method! Just pass in the value you want to check for. The method will return a true or false whether the value is contained in it.

print(someSet.contains(1));

Run the program. You’ll see that the set does contain a one value. If you want to add elements to a set, the add method is what you’re looking for. Because sets aren’t ordered, you don’t need to specify where you’re inserting the new value, you just pass the value in as the only argument.

someSet.add(5);

Now five is part of the set! To remove an item, use the remove method.

someSet.remove(3);

When you want to iterate over a set, you can use just a regular for-in loop. Remember, a set doesn’t hold its items in order. It may list in order, but that’s not guaranteed at all. Add the following:

for (var item in someSet) {
    print(item);
}

Now run the program. You’ll see a list of values printed to the console. Okay, so on face value, a set behaves almost like a list, except the values are unordered and there are no duplicates. So what else does it provide. Well, I’m glad you asked, and I’ll show you in the next episode.