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.
1 |
var setName = <type>{}; |
Or
1 |
Set<type> setName = {}; |
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.
1 |
// var names = {}; // Creates a map, not a set. |
Example:-
1 2 3 4 5 |
void main() { var persons = <String>{"John", "Doe", "Smith", "Alex"}; print("W3Adda - Dart Declare/Initialize Set."); print(persons); } |
Output:-
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:-
1 |
set_name.add(<value>); |
Example:-
1 2 3 4 5 6 7 8 9 |
void main() { var names = {"John", "Doe", "Smith", "Alex"}; var persons = <String>{}; print("W3Adda - Dart insert Item(s) into Set."); persons.add("Murphy"); print(persons); persons.addAll(names); print(persons); } |
Output:-
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:-
1 |
set_name.elementAt(<index>); |
Example:-
1 2 3 4 5 6 |
void main() { var persons = <String>{"John", "Doe", "Smith", "Alex"}; var p = persons.elementAt(2); print("W3Adda - Dart Get Item at Index."); print(p); } |
Output:-
Dart Get Set Elements Count
In Dart lenth property can be used to find the number of elements in a set.
Syntax:-
1 |
set_name.length; |
Example:-
1 2 3 4 5 6 |
void main() { var persons = <String>{"John", "Doe", "Smith", "Alex"}; var l = persons.length; print("W3Adda - Dart Get Set Length."); print(l); } |
Output:-
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:-
1 |
set_name.contains(<value>) |
Example:-
1 2 3 4 5 6 7 8 9 10 |
void main() { var persons = <String>{"John", "Doe", "Smith", "Alex"}; print("W3Adda - Dart find an Item in set."); if(persons.contains("Doe")){ print("Given element found."); } else{ print("Given element not found."); } } |
Output:-
Dart Remove Set Element
In Dart remove() method is used to remove or delete an element from given set.
Syntax:-
1 |
set_name.contains(<value>) |
Example:-
1 2 3 4 5 6 7 8 9 |
void main() { var persons = <String>{"John", "Doe", "Smith", "Alex"}; print("W3Adda - Dart remove an element from set."); print("Before Delete"); print(persons); print("After Delete"); persons.remove("Doe"); print(persons); } |
Output:-
Dart Iterating Over a Set Elements
In Dart, we can loop through the set elements using forEach method as following –
Example:-
1 2 3 4 5 6 7 |
void main() { var persons = <String>{"John", "Doe", "Smith", "Alex"}; print("W3Adda - Dart Iterating Set Elements."); persons.forEach((value) { print('Value: $value'); }); } |
Output:-
Dart Remove all Set Elements
The clear() method is used to remove or delete all from given set.
Syntax:-
1 |
set_name.clear(); |
Example:-
1 2 3 4 5 6 7 8 9 |
void main() { var persons = <String>{"John", "Doe", "Smith", "Alex"}; print("W3Adda - Dart remove all elements from set."); print("Before Clear"); print(persons); print("After Clear"); persons.clear(); print(persons); } |
Output:-
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:-
1 |
List<type> <list_name>= <set_name>.toList(); |
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:-
1 2 3 4 5 6 7 8 9 10 11 12 |
void main() { var a = <int>{10,12,14,16,18}; var b = <int>{5,7,9,11,13}; var c = <int>{2,3,5,7}; print("W3Adda - Dart Set Operations"); print("b union a is"); print(b.union(a)); print("b intersection a is"); print(b.intersection(a)); print("b difference c is"); print(b.difference(c)); } |
Output:-
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. |