Dart Sets

In this tutorial you will learn about the Dart Sets and its application with practical example.

Dart Sets

In Dart, Set is a unordered list of distinct values of same types. Set is much similar to an array but it is unordered and won’t allows duplicate elements, each value contained in a set is unique. Dart sets are typed, thus once you declare the type of the set or Dart infers it then you would only have elements of the same type. Sets are useful when we want to hold distinct values of single data type in single variable and order of items is not important.

Dart Declaring/Initialize Set

There are two ways we can declare/initialize an empty set, use {} preceded by a type argument, or assign {} to a variable of type Set.

Or

Here, SetName is replaced with the name of set variable and type is replaced with the data type of set.

Note :- The syntax for map literals is similar to that for set literals. If you forget the type annotation with {} or with the variable it’s assigned to, then Dart would creates an Map object instead of Set.

Example:-

Output:-

dart_declaring_set

Add Element Into Set

In Dart, add() or addAll() function is used to add or insert item(s) into given set. The add() method is used to insert single item into an existing set while addAll() method is used to add multiple items into the given set. Duplicate value in a set will be ignored.

Syntax:-

Example:-

Output:-

dart_add_item_in_set_add_method_new

Dart Get Set Element at Index

The elementAt() method is used to get the item at specified index position. Indexing of a Set starts from zero (0) to the last element of Set which is size – 1 where size is the number of elements in a set. If you enter a number bigger than maximum index it will throw an error.

Syntax:-

Example:-

Output:-

dart_get_item_at_index

Dart Get Set Elements Count

In Dart lenth property can be used to find the number of elements in a set.

Syntax:-

Example:-

Output:-

dart_get_set_length

Dart Finding Element in a Set

Dart contains() method can be used to find an element in a set, it takes a single element of the same type to be find within the set and returns a boolean value to indicate given element is exists or not.

Syntax:-

Example:-

Output:-

dart_set_find_using_contains

Dart Remove Set Element

In Dart remove() method is used to remove or delete an element from given set.

Syntax:-

Example:-

Output:-

dart_remove_set_element

Dart Iterating Over a Set Elements

In Dart, we can loop through the set elements using forEach method as following –

Example:-

Output:-

dart_iterating_set_elements

Dart Remove all Set Elements

The clear() method is used to remove or delete all from given set.

Syntax:-

Example:-

Output:-

dart_clear_remove_all_set_elements

Dart Convert Set to List

Dart toList()method is used to convert a Set object to List object. The type of the List must be the same as the type of Set elements.

Syntax:-

Dart Set Operations

In Dart, we can perform some of the following basic set operations on any Set –

Union :- The union of two sets a and b is the set combining values of a and b.

intersection :- The intersection of two sets a and b is a set that contains all elements common in both sets.

subtracting :- The subtraction of two sets a and b is set containing all elements of set a and removing elements that belongs to set b.

Example:-

Output:-

dart_set_operations

Dart Set Properties

Below is a list of properties supported by Dart Sets.

Property Description
first It returns the first element in set.
isEmpty It returns true if the set has no elements.
isNotEmpty It returns true if the set has at least one element.
length It returns length/size of the set, can also be seen as number of elements in a given set.
last It returns the last element in the set.
hashCode It returns an hash code for the corresponding object.
Single It is used to checks if the set has only one element and returns it.

In this tutorial we have learn about the Dart Sets and its application with practical example. I hope you will like this tutorial.