The Set
is to create an unordered collection of unique items.
Because a set is unordered, you can’t get a Set items by index (position)
You can create Set
in following ways:
The Set class also defines several methods for manipulating set values or variables, like, add, remove, contains etc.
The set literal is to create a single object of a set.
main() {
// create a set using set literal.
var setItems = {'Item1', 'Item2'};
setItems.add('Item1');
setItems.add('Item2');
}
Note: Sets only contains unique items, the following code removes the duplicate and results with unique items.
main() {
var setItems = {'Item1', 'Item1'};
print(siteNavbar); // {'Item1'}
}
The set constructor is to create object of a Set that can be initiated into multiple instances with new
keyword.
The following code snippet shows how to create a set with its constructor.
main() {
// create a set using its constructor.
var setItems = Set();
setItems.add('Item1');
setItems.add('Item2');
}
The parameterized type set literal is to create a single object of a Set by explicitly defining the data type to the Set, which only allows the values of same type objects.
Use syntax like <data type>
to declare:
The following code snippet shows how to create a set with specific type with set literal.
// use {} preceded by a type argument
var setItems = <String>{'Item1', 'Item2'};
// OR
Set<String> setItems = {'Item1', 'Item2'};
setItems.add(2); // error 'int' can't be assigned to the parameter type 'String'
The parameterized type set constructor is to create object of a Set by explicitly defining the data type to the Set, which only allows the values of same type objects.
Use syntax like List<data type>
to declare:
The following code snippet shows how to create a set with specific type with set constructor.
var setItems = Set<String>();
// OR
Set<String> setItems = Set();
setItems.add(2); // error 'int' can't be assigned to the parameter type 'String'
The constant Set is to create unmodified set, means it cannot be modified once created.
Attempting to modify a constant set literal will result in a dynamic error.
It can be defined using const
keyword
The following code snippet shows how to create a constant set.
main() {
var setItems = const {'Item1', 'Item2'};
setItems.add('Item3'); // Runtime error Cannot change unmodifiable set.
}
The spread operator(...) is to insert a set of objects in other set.
The following code snippet shows how to insert set of objects in other set.
main() {
var setOfElements1 = {'Item1', 'Item2'};
var setOfElements2 = {...setOfElements1, 'Item3'};
print(setOfElements2); // {Item1, Item2, Item3}
}
The Null-aware spread operator(...?) is to avoid runtime exception while inserting a null set object in to other set.
Use ...?
operator.
The following code snippet shows how to avoid runtime exception while insert null
set object in other set.
var setItems1; // implicitly null
var setItems2 = {...?setItems1, 'Item3'};
print(setItems2); // {Item3}
The collection if is to build Sets using if
condition.
Manipulate the item of a Set before adding them.
The following code snippet shows how to build a set using collection if
.
bool isThirdItemAvailable = true;
var setItems = {
'Item1',
'Item2',
if (isThirdItemAvailable) 'Item3',
};
print(setItems); // {Item1, Item2, Item3}
The collection for
is to build sets using repetition for
condition.
Manipulate the items of a sets before adding them to another sets:
The following code snippet shows how to build a set using collection for
.
var setItems = {'Item2', 'Item3', 'Item4'};
var otherSetItems = {'Item1', for (var item in setItems) item.toString()};
print(otherSetItems); // {Item1, Item2, Item3, Item4}
The add()
method is to add value
to the end of this set.
Its extends the length of set by one.
The following code snippet shows how to add a new value to this set.
main() {
var setObject = {'Item1', 'Item2'};
// add new value to list
setObject.add('Item3');
// print elements of this list to console.
print(setObject); // [Item1, Item2, Item3]
}
The addAll()
method is to append the set of objects to the end of this set.
It extends the length of the set by the number of objects in iterable
.
The following code snippet shows how to add the set of objects to this set.
main() {
var setObject = {'Item1', 'Item2'};
// Append the set of object to this set
setObject.addAll({'Item3', 'Item4'});
// print objects of this set
print(setObject); // [Item1, Item2, Item3, Item4]
}
The remove()
method is to remove the first occurrence of value
from this set.
Returns true
if value
was in the set, otherwise return false
.
The following code snippet shows how to remove the element from the set by value.
main() {
var setObject = {'Item1', 'Item2', 'Item3'};
// return true or false
var isRemoved = setObject.remove('Item2');
// it print true because the given value was in the set
print(isRemoved); // true
// it print remaining elements of set after removing the value from the set.
print(setObject); // [Item1, Item3]
}
Note: The method has no effect if value
was not in the set.
// return true or false
var isRemoved = setObject.remove('Item4');
// it prints false, because the given value was not in the set.
print(isRemoved); // false
The removeAll()
method is to removes each element of collection or elements from this set.
The following code snippet show how to remove set of elements from the set.
main() {
var setObject = {'Item1' 'Item2', 'Item3'};
setObject.removeAll({'Item1' 'Item2'});
print(setObject); // {Item3}
}
Example: Demo code
main() {
//create a set using set literal
var setItems = {'Item1', 'Item2'};
// Add a value
setItems.add('Item3');
// Add list of values
setItems.addAll(['Item4', 'Item5']);
// remove a value
setItems.remove('Item3');
// remove list of values
setItems.removeAll(['Item4', 'Item5']);
print(setItems);
}
To create an empty set, use preceded by a type argument, or define a variable with type.
// use {} preceded by a type argument
var siteNavbar = <String>{}; // Works
// define a variable with type
Set<String> siteNavbar2 = {}; // This works, too.
}
The contains()
method is to check whether a value is in the set. Returns true
if value
is in the set. Otherwise returns false
.
The following code snippet shows how to check, does this set contains a given value.
var setItems = {'Item1' 'Item2'};
if (setItems.contains('Item2')) {
print('The value is in a set ');
}
print(setItems.contains('Item3')); // false
The containsAll()
method is to check whether this Set contains all the elements of other set. Returns true
if other set is in the set. Otherwise returns false
.
The following code snippet shows how to check, does this set contains all the elements of other set.
main() {
var setObject = {'Item1' 'Item2'};
// check whether this set contains all the object of given set.
if (setObject.containsAll(['Item1' 'Item2'])) {
print('The value is in a set ');
}
print(setObject.containsAll(['Item1' 'Item3'])); // false
}
The forEach()
method is to apply a function to each element of the set in iteration order.
The following code snippet shows how to apply print function to each element of the set to print to console.
main() {
var skills = {'Javascript', 'Angular', 'React'};
skills.forEach((skill) => print('$skill Tutorial'));
// Javascript Tutorial
// Angular Tutorial
// React Tutorial
}
The clear()
method is to remove all objects from this set; the length of the set becomes zero.
The following code snippet shows how to remove all the elements of the set.
main() {
var setObject = {'Item1', 'Item2', 'Item3'};
setObject.clear();
print(setObject); // []
}
The map()
method is to return a new lazy Iterable with elements that are created by calling function
on each element of the set.
The following code snippet shows how to return a new Iterable by making all the elements to upper case.
main() {
var skills = {'Javascript', 'Angular', 'React'};
var result = skills.map((skill) => skill.toUpperCase());
result.forEach(print);
// JAVASCRIPT ANGULAR REACT
}
The where()
method is to filter the set and return the matching elements. It returns a new lazy iterable.
The matching elements have the same order in the returned iterable as they have in [iterator]
The following code snippet shows how to filter set to return matching elements.
main() {
var skills = {'Javascript', 'Angular', 'React'};
var result = skills.where((skill) => skill == 'React');
// returns a new lazy iterable, check its runtime type.
result.runtimeType; // Iterable<String>
// print to console
result.forEach(print); // React
}
The any()
method is to check whether any item of this set satisfies condition([test]).
It checks every element in iteration order, and returns true
if any of them satisfies the condition([test]), otherwise returns false
.
The following code snippet shows how to check, in this set does any item satisfies given test.
main() {
var skills = {'Javascript', 'Angular', 'React'};
if (skills.any((skill) => skill.contains('React'))) {
print('The list satisfies give test');
}
print(skills.any((skill) => skill.contains('React'))); // true
}
The every()
method is to check whether every element of this set satisfies given condition(test
).
It checks every element in iteration order, and returns false
if any of them not satisfies the condition([test]), otherwise returns true
.
The following code snippet shows how to check, in this does every element satisfies given test.
main() {
var skills = {'Javascript', 'Angular', 'React'};
if (skills.every((skill) => skill.contains('a'))) {
print('The set satisfies give test');
}
print(skills.every((skill) => skill.contains('a'))); // true
}