Set is a special type of collection in Dart programming. In which any object can occur only once. If we perform add operation in the set, if the object is already in the set then it will not add otherwise it will be added in the set. dart: core library provides the Set class for implementation.
Syntax: Identifier = new Set() or Identifier = new Set.from(Iterable)
Here, Iterable is the list of value which will be added in the set .
Example 1:
void main() {
// create a new set number
Set number = new Set();
// add in the set
number.add(1);
number.add(2);
number.add(5);
number.add(1);
// all elements are retrieved
// in the order in which
// they are inserted
for(var no in number) {
print(no);
}
}
Output:
1 2 5
Example 2:
void main() {
// Create a Set Number Which
// contains a list of value
Set number = new Set.from([1,11,14,11]);
// all elements are retrieved
// in the order in which
// they are inserted
for(var n in number) {
print(n);
}
}
Output:
1 11 14
Example 3:
void main() {
// Create a Set Number Which
// contains a list of value
Set number = new Set.from(['geeks','for','geeks']);
// all elements are retrieved
// in the order in which
// they are inserted
for(var n in number) {
print(n);
}
}
Output:
geeks for