Dart Set

Summary: in this tutorial, you’ll learn about the Dart Set class and how to manipulate sets effectively.

Introduction to the Dart Set

A set is a collection of unique elements. Unlike a list, a set doesn’t allow duplicates. Also, a set doesn’t maintain the order of elements. Typically, a set is faster than a list, especially when working with large elements.

Dart uses the Set<E> class to manage sets. Like the List<E> class, the Set<E> class implements the Iterable class.

Creating a set

To create an empty set, you use empty curly braces. For example, the following creates an empty set of integers:

Set<int> ratings = {};Code language: Dart (dart)

Or you can move the type to the right-hand side:

var ratings = <int>{};Code language: Dart (dart)

The following example creates a set of integers and displays it using the print() function:

void main() {
  var ratings = {1, 2, 3};
  print(ratings);
}Code language: Dart (dart)

Output:

{1, 2, 3}Code language: Dart (dart)

Getting the number of elements

To find the number of elements of a set, you use the length property. For example:

void main() {
  var ratings = {1, 2, 3};
  print('Length: ${ratings.length}');
}Code language: Dart (dart)

Output:

Length: 3Code language: Dart (dart)

Accessing an element by index

Unlike a list, you cannot access an element at an index using square brackets []. Instead, you can use the elementAt() method like this:

void main() {
  var ratings = {1, 2, 3};
  print(ratings.elementAt(0));
  print(ratings.elementAt(1));
  print(ratings.elementAt(2));
}Code language: Dart (dart)

Output:

1
2
3Code language: Dart (dart)

Also, you can use the first and last property to access the first and last elements respectively. For example:

void main() {
  var ratings = {1, 2, 3};
  print(ratings.first);
  print(ratings.last);
}Code language: Dart (dart)

Output:

1
3Code language: Dart (dart)

Adding an element to a set

To add an element to a set, you use the add() method. For example, the following uses the add() method to add the numbers 4 and 5 to the ratings set:

void main() {
  var ratings = {1, 2, 3};
  ratings.add(4);
  ratings.add(5);
  print(ratings);
}Code language: Dart (dart)

Notice that if you add an element that already exists in a set, the set will contain one element as expected. For example:

void main() {
  var ratings = {1, 2, 3};
  ratings.add(1);
  print(ratings);
}Code language: Dart (dart)

Output:

{1, 2, 3}Code language: Dart (dart)

Adding multiple elements

To add multiple elements from a list to a set, you use the addAll() method. For example:

void main() {
  var ratings = {1, 2, 3};
  ratings.addAll([4, 5]);
  print(ratings);
}Code language: Dart (dart)

Output:

{1, 2, 3, 4, 5}Code language: Dart (dart)

Checking the existence of elements

To check if an element is in a set, you use the contains() method. The contains() method returns true if a set contains an element. Otherwise, it returns false. For example:

void main() {
  var ratings = {1, 2, 3};
  print(ratings.contains(1)); // true
  print(ratings.contains(4)); // false
}Code language: Dart (dart)

Output:

true
trueCode language: Dart (dart)

Finding the intersection of two sets

The intersection of two sets returns a set that contains the elements that are in both sets. For example:

void main() {
  var a = {1, 2, 3};
  var b = {2, 3, 4};
  var c = a.intersection(b);
  print(c);
}Code language: Dart (dart)

Output:

{2, 3}Code language: Dart (dart)

Finding the union of two sets

The union of two sets returns unique elements that are in both sets. For example:

void main() {
  var a = {1, 2, 3};
  var b = {2, 3, 4};
  var c = a.union(b);
  print(c);
}Code language: Dart (dart)

Output:

{1, 2, 3, 4}Code language: Dart (dart)

Iterating over elements of a set

To iterate over elements of a set, you use the for-in loop. For example:

void main() {
  var ratings = {1, 2, 3, 4, 5};
  for (var rating in ratings) {
    print(rating);
  }
}Code language: Dart (dart)

Output:

1
2
3
4
5Code language: Dart (dart)

Since the Set class has the length property that returns the number of elements of a set, you can loop over the elements using the for loop:

void main() {
  var ratings = {1, 2, 3, 4, 5};
  for (var i = 0; i < ratings.length; i++) {
    print(ratings.elementAt(i));
  }
}Code language: Dart (dart)

Output:

1
2
3
4
5Code language: Dart (dart)

Summary

  • A set is a collection of unique elements. A set doesn’t maintain the order of elements.
  • Use the add() method to add an element to a set.
  • Use the addAll() method to add elements from a list to a set.
  • Use the remove() method to remove an element from a set.
  • Use the intersection() method to find the intersection of two sets.
  • Use the union() method to find the union of two sets.
  • Use the for-in to iterate over elements of a set.
Was this tutorial helpful ?