Dart Sets
Dart Set is a collection of elements, where order of elements is not recorded, and the elements must be unique.
For example, the following Set of integers is a valid Set.
{2, 4, 6, 0, 1, 8, 9}
The following Set is not a valid Set, since the element 2
occurred twice.
{2, 4, 2, 0}
Define Set
We can define a Set variable in Dart using Set
keyword.
Set mySet;
We have not specified the type of elements we store in this Set. If type is not specified, then it would be inferred as dynamic.
We may specify the type of elements we store in the Set, as shown in the following.
Set<int> mySet
Initialize Set
We can initialize a set by assigning the Set variable with the elements enclosed in curly braces.
Set<int> mySet = {2, 4, 6, 0, 1, 8, 9};
Or we may create an empty Set using Set()
constructor, and then add the elements using Set.add()
method.
Set mySet = Set();
mySet.add(2);
mySet.add(4);
Set Tutorials
The following tutorials cover more topics on Sets in Dart programming.
- Dart – Set Length
- Dart – Check if Set is Empty
- Dart – Add Element to Set
- Dart – Check if Set contains given Element
- Dart – Filter Elements of a Set
- Dart – Remove given Element from Set
- Dart – Remove Elements from Set based on a Condition
- Dart – Union of Sets
- Dart – Intersection of Sets
- Dart – Iterate over elements of Set using For-in
- Dart – Iterate over elements of Set using forEach()
- Dart – Reduce Elements of Set to a Value
Summary
In this Dart Tutorial, we learned about Sets, and different methods, properties, and concepts related to Sets, with well explained and dedicated tutorials.