The Map is to create unordered collection elements of key-value pairs.
A map, commonly known as a dictionary or hash.
You can create Map in following ways:
The Map class also defines several methods for manipulating set values or variables, like, putIfAbsent, remove, containsKey, containsValue etc.
The Map literal is to create a single object of a map like singleton.
The following code snippet shows how to create a map object using map literal.
var mapItems = {
// Key Value
101, 'Item1',
102, 'Item2',
103, 'Item3',
};
The Map constructor is to create object of a map that can be initiated into multiple instances with new keyword.
The following code snippet shows how to create a map object using map constructor.
main() {
var mapItems = Map();
mapItems[101] = 'Item1';
mapItems[102] = 'Item2';
}
The parameterized type Map literal is to create Map by explicitly defining the data type to the map, which only allows the values of same type objects.
The following code snippet shows how to create a map with specific type key and value pairs using map literal.
var mapItems = <int, String>{
101: 'Item1',
102: 'Item2',
103: 'Item3',
};
The parameterized type Map constructor is to create Map by explicitly defining the data type to the map, which only allows the values of same type objects.
The following code snippet shows how to create a map with specific type key and value pairs using map constructor.
var mapItems = Map<int, String>();
mapItems[101] = 'Item1';
mapItems[102] = 'Item2';
Both keys and values can be any type of object:
Example: Keys and values both are strings type.
The following snippet shows key and values both are string type.
var mapItems = <String, String>{
'first': 'Item1',
'middle': 'Item2',
'last': 'Item3',
};
Each key occurs only once:
Example: if you use same key multiple times, compiler consider the order is defined by first occurrence, but the value is defined by the last.
The following code snippet shows two keys in a map literal shouldn't be equal. Change or remove the duplicate key.
var mapItems = <int, String>{
101: 'Item1',
101: 'Item2',
103: 'Item3',
};
print(mapItems); // {101: Item2, 103: Item3}
You may use the same value multiple times.
Example: Item1
value used multiple times.
The following code snippet shows that map may contains duplicate values.
var mapItems = <int, String>{
101: 'Item1',
102: 'Item1',
103: 'Item2',
};
If you look for a key that isn’t in a map, it return null.
The following code snippet shows that if key does not exists in the map, it return null.
var mapItems = <int, String>{
101: 'Item1',
102: 'Item1',
103: 'Item2',
};
mapItems[104]; // null
The constant map, is to create a unmodifiable map, that cannot be modified once they are created.
A constant map literal evaluated at compile time. Attempting to modify a constant map literal will result in a dynamic error.
It can be defined using const
keyword
The following code snippet shows how to create a constant map.
main() {
var constMap = const {
101: 'Item1',
102: 'Item2',
};
// Error
constMap[103] = 'Item3';
// Runtime error Cannot set value in unmodifiable Map
}
The spread operator(...) is to insert the map object in to other map objects
The following code snippet shows how to insert map object in to other map.
main() {
var primaryMap = {
101: 'Item1',
102: 'Item2',
};
// use ... operator
var secondaryMap = {...primaryMap, 103: 'Item3'};
print(secondaryMap); // {101: Item1, 102: Item2, 103: Item3}
}
The Null-aware spread operator(...?) inside map is to avoid runtime exception while inserting a null map object in to another map.
The following code snippet shows how to avoid runtime exception while inserting null map object in to other map.
main() {
var primaryMap; // null
var secondaryMap = {...?primaryMap, 103: 'Item3'};
print(secondaryMap); // {103: Item3}
}
The collection if
is to build map using if
condition.
Manipulate the item of a map before adding them.
The following code snippet shows how to build a map using if condition.
bool isThirdItemAvailable = true;
var mapItems = {
101: 'Item1',
102: 'Item2',
if (isThirdItemAvailable) 103: 'Item3',
};
print(mapItems); // {101: Item1, 102: Item2, 103: Item3}
The putIfAbsent()
method is to add a new value if it isn't there or look up the value of key
.
Returns the value associated to key, if there is one. Otherwise calls [ifAbsent] to get a new value, associates [key] to that value, and then returns the new value.
The following code snippet shows how to put new pairs in map.
Map<int, String> mapItems = {101: 'Item1'};
var counter = 101;
for (var item in ['Item2', 'Item3']) {
int key = ++counter;
mapItems.putIfAbsent(key, () => item);
}
print(mapItems); // {101: Item1, 102: Item2, 103: Item3}
The length
property is to get the count, i.e. number of key/value pairs in the map.
The following code snippet shows how to get the length of map object.
main() {
var mapItems = {
101: 'Item1',
102: 'Item2',
103: 'Item3',
};
mapItems.length; // 3
print(mapItems.length); // 3
}
The isEmpty
property is to check for empty map, returns true if there is no key/value pair in the map.
The following code snippet shows how to check this set is empty.
main() {
var mapItems = {};
mapItems.isNotEmpty; // true
if (mapItems.isEmpty) {
print('This set is empty');
}
}
The isNotEmpty
property is to check for non empty map, returns true if there is at least one key/value pair in the map.
The following code snippet shows how to check this set is not empty.
main() {
var mapItems = {
101: 'Item1',
102: 'Item2',
103: 'Item3',
};
mapItems.isNotEmpty; // true
if (mapItems.isNotEmpty) {
print('This set is not empty');
}
}
The keys
property of a Map is to get all the keys as an unordered collection (an Iterable).
The following code snippet shows how to get all the keys from map object.
main() {
var mapItems = {
101: 'Item1',
102: 'Item2',
103: 'Item3',
};
mapItems.keys;
print(mapItems.keys); // (101, 102, 103)
}
The values
property of Map is to get all the values as an unordered collection (an Iterable of Lists).
The following code snippet shows how to get all values from map object.
main() {
var mapItems = {
101: 'Item1',
102: 'Item2',
103: 'Item3',
};
mapItems.values;
print(mapItems.values); // (Item1, Item2, Item3)
}
The containsKey()
method is to check whether the map contains the given key
. Returns true if key present in map, Otherwise return false.
The following code snippet shows how to check whether map contains a given key
.
main() {
var mapItems = {
101: 'Item1',
102: 'Item2',
103: 'Item3',
};
if (mapItems.containsKey(102)) {
print('The map contains given key');
}
}
The containsValue()
method is to check whether the map contains the given value
. Returns true if value present in map. Otherwise return false.
The following code snippet shows how to check whether map contains a given value
.
main() {
var mapItems = {
101: 'Item1',
102: 'Item2',
103: 'Item3',
};
if (mapItems.containsValue('Item2')) {
print('The map contains given value');
}
}
The foreach()
method is to apply on a function to each key/value pair of the map.
When you invoke forEach() on a map, your function must take two arguments (the key and value)
The following code snippet shows how to apply print function on each key/value pair of map.
main() {
var mapItems = {
101: 'Item1',
102: 'Item2',
103: 'Item3',
};
mapItems.forEach((key, value) {
print('The value of key ${key} is ${value}');
});
}
The remove()
method is to remove key and its associated value from the map, if present
Returns the value associated with key
before it was removed. Returns null
if key
was not in the map.
The following code snippet shows how to remove value from map by using its associated key.
main() {
var mapItems = {
101: 'Item1',
102: 'Item2',
103: 'Item3',
};
// remove value from map by using its key.
mapItems.remove(102);
// print remaining pairs to console.
print(mapItems); // {101: Item1, 103: Item3}
}
Note: that values can be null
and a returned null
value doesn't always mean that the key was absent.
The clear()
method is to removes all pairs from the map. After this, the map is empty.
The following code snippet shows how to remove all pairs from map.
main() {
var mapItems = {
101: 'Item1',
102: 'Item2',
103: 'Item3',
};
// Removes all pairs from the map.
mapItems.clear();
print(mapItems); // {} empty map
}