Overview of Maps in Dart
Maps in Dart are a collection of key-value pairs that store the data in an unordered manner. In this section, we will discuss the different ways to declare and initialize a Map in Dart.
Declaring a Map using Map Literals
Map Literals provide a concise way to create a Map in Dart. To declare a map using Map Literals, we need to enclose the key-value pairs within a pair of curly brackets “{ }”.
Syntax:
The syntax to declare a Map using Map Literals is as follows −
var identifier = { key1:value1, key2:value2 [,…..,key_n:value_n] }
Example:
Here is an example of declaring a Map using Map Literals −
void main() {
var details = {'Usrname':'tom','Password':'pass@123'};
print(details);
}
Output:
{Usrname: tom, Password: pass@123}
Adding Values to Map Literals at Runtime
We can add values to Map Literals at runtime by assigning a key-value pair to it using the subscript operator [].
Example:
Here is an example of adding a key-value pair to a Map Literal at runtime −
void main() {
var details = {'Usrname':'tom','Password':'pass@123'};
details['Uid'] = 'U1oo1';
print(details);
}
Output:
{Usrname: tom, Password: pass@123, Uid: U1oo1}
Declaring a Map using a Map Constructor
To declare a Map using a Map Constructor, we have two steps. First, declare the Map, and second, initialize the Map using the Map() constructor.
Syntax:
The syntax to declare a Map using a Map Constructor is as follows −
var identifier = new Map()
Example:
Here is an example of declaring a Map using a Map Constructor −
void main() {
var details = new Map();
details['Usrname'] = 'tom';
details['Password'] = 'pass@123';
print(details);
}
Output:
{Usrname: tom, Password: pass@123}
Properties
No | Property & Description |
1 | Keys Returns an iterable object representing keys |
2 | Values Returns an iterable object representing values |
3 | Length Returns the size of the Map |
4 | isEmpty Returns true if the Map is an empty Map |
5 | isNotEmpty Returns true if the Map is an empty Map |
Example:
void main() {
var myMap = {'name': 'John', 'age': 30, 'city': 'New York'};
// accessing individual key-value pairs
print(myMap['name']); // John
print(myMap['age']); // 30
print(myMap['city']); // New York
// accessing keys and values as iterables
print(myMap.keys); // (name, age, city)
print(myMap.values); // (John, 30, New York)
// getting the size of the Map
print(myMap.length); // 3
// checking if the Map is empty or not
print(myMap.isEmpty); // false
print(myMap.isNotEmpty); // true
// removing a key-value pair
myMap.remove('age');
print(myMap); // {name: John, city: New York}
}
Output:
John
30
New York
(name, age, city)
(John, 30, New York)
3
false
true
{name: John, city: New York}
Functions
No | Function Name & Description |
1 | addAll() Adds all key-value pairs of others to this map. |
2 | clear() Removes all pairs from the map. |
3 | remove() Removes key and its associated value, if present, from the map. |
4 | forEach() Applies f to each key-value pair of the map. |
Example:
void main() {
var map1 = {'a': 1, 'b': 2};
var map2 = {'c': 3, 'd': 4};
// Adding all key-value pairs of map2 to map1
map1.addAll(map2);
print(map1); // Output: {a: 1, b: 2, c: 3, d: 4}
// Clearing all key-value pairs of map1
map1.clear();
print(map1); // Output: {}
var map3 = {'e': 5, 'f': 6};
// Adding some key-value pairs to map3
map3['g'] = 7;
map3['h'] = 8;
// Removing a key-value pair from map3
map3.remove('e');
print(map3); // Output: {f: 6, g: 7, h: 8}
// Applying a function to each key-value pair of map3
map3.forEach((key, value) {
print('$key: $value');
});
// Output:
// f: 6
// g: 7
// h: 8
}
Output:
{a: 1, b: 2, c: 3, d: 4}
{}
{f: 6, g: 7, h: 8}
f: 6
g: 7
h: 8