A collection of objects in which each object can occur only once.
That is, for each object of the element type, the object is either considered to be in the set, or to not be in the set. Set implementations may consider some elements indistinguishable. These elements are treated as being the same for any operation on the set.
The default Set implementation, LinkedHashSet, considers objects indistinguishable if they are equal with regard to Object.== and Object.hashCode.
Iterating over elements of a set may be either unordered or ordered in some way. Examples:
- A HashSet is unordered, which means that its iteration order is unspecified,
- LinkedHashSet iterates in the insertion order of its elements, and
- a sorted set like SplayTreeSet iterates the elements in sorted order.
It is generally not allowed to modify the set (add or remove elements) while an operation on the set is being performed, for example during a call to forEach or containsAll. Nor is it allowed to modify the set while iterating either the set itself or any Iterable that is backed by the set, such as the ones returned by methods like where and map.
Let’s create a simple set
Code 1:
void main(){ var set1={10,10,10,20,20,30,30,30}; print(set1); print(set1.runtimeType); } |

Let’s create a String set and print type.
code 2:
void main(){ var SET={‘appale’,’appale’,’appale’,’google’,’google’,’google’}; print(SET); print(‘SET type ->’); print(SET.runtimeType); } |

Another way to create set…
Code 3:
void main(){ // integer type Set final Set=<int>{1,1,1,2,2,2,4,4,5,5}; print(‘integer set is: $Set’); // double type Set final DoSet=<double>{1.1,1.1,1.1,2.5,2.5,5.7,5.7}; print(‘double set is: $DoSet’); // string type Set final StrSet=<String>{‘a’,’a’,’a’,’bc’,’bc’,’gf’,’gf’,’gf’}; print(‘String set is: $StrSet’); } |

Set Operations
We can’t do string merging & concatenation on set.
Add Data
We can add data into set using add(), addAll() method. Main difference, using add() method we can adds one element at a time into set, but using addAll() method we can adds multiple elements into set. Let’s see below code…
Code 4:
void main(){ var Set={1,1,1,1}; print(‘Set is $Set’); // add element into set Set.add(2); Set.add(6); print(‘Set now $Set’); } |

code 5:
void main(){ var Set={1,1,1,1}; print(‘Set is $Set’); // add multiple element into set Set.addAll({2,3,4}); print(‘Set now $Set’); } |

Set length & contains
We can find set length using ‘.length’ and an element/item contains inside a set or not can be found using .contains() method.
Code 6:
void main(){ var sEt={1,1,1}; print(‘set is $sEt’); print(‘set length ‘); print(sEt.length); sEt.add(2); sEt.add(3); sEt.add(4); print(‘set now $sEt’); print(‘set length ‘); print(sEt.length); } |

code 7:
void main(){ var set1={20,20,20,40,40,-5}; print(‘set is: $set1’); print(set1.contains(-5)); print(set1.contains(20)); } |

Remove elements
We can remove elements from set. You can remove duplicate and non-duplicate elements also.
For this you can use .remove() method.
Code 8:
void main(){ var set1={20,20,20,40,40,-5,40,40}; print(‘set is: $set1’); set1.remove(-5); print(‘set now $set1’); set1.remove(20); print(‘set now $set1’); } |

Set empty?
We can check a set is empty or not using .isEmpty method.
Code 9:
void main(){ var set1={20,20,20,40,40,-5,40,40}; print(‘set is: $set1’); print(set1.isEmpty); set1.remove(20); print(‘set is: $set1’); print(set1.isEmpty); set1.remove(40); print(‘set is: $set1’); print(set1.isEmpty); set1.remove(-5); print(‘set is: $set1’); print(set1.isEmpty); } |
