Dart Map
A Dart Map is a type of object used to store key-value pairs of data. Every value has a key that corresponds to it and is used to access the relevant value. Values and keys might be of any kind. While every key in a dart map must be distinct, the same value may appear more than once. The Python Dictionary and the Map representation are somewhat similar. Curly braces {} can be used to declare the map, and commas(,) are used to separate each key-value pair. A square bracket([]) can be used to obtain the key’s value.
Declaring a Dart Map
There are two ways to define a dart map.
- Using Map Literal
- Using Map Constructor
The following is the syntax for declaring a Dart map.
Using Map Literals
The curly braces “{}” surround the key-value pairs, which are then separated by commas to declare a map using a map literal. Below is the syntax.
Syntax
var map_name = {key1:value1, key2:value2 [.......,key_n: value_n]}
Example
void main() {
var student = {'name':'Tom','age':'23'};
print(student);
}
Output
{name: Tom, age: 23}
Example
void main() {
var student = {'name':' tom', 'age':23};
student['course'] = 'B.tech';
print(student);
}
Output
{name: tom, age: 23, course: B.tech}
Explaination
We declared a map with the name of a student in the example above. Using a square bracket, we added the value at runtime and passed the new key along as a course linked to its value.
Using Map Constructor
There are two methods for declaring the Dart Map using the map constructor. First, use the map() constructor to declare a map. Initialize the map second. Below is the syntax.
Syntax
var map_name = new map()
Initialize the settings after that.
map_name[key] = value
Example
void main() {
var student = new Map();
student['name'] = 'Tom';
student['age'] = 23;
student['course'] = 'B.tech';
student['Branch'] = 'Computer Science';
print(student);
}
Output
{name: Tom, age: 23, course: B.tech, Branch: Computer Science}
Map Properties
The dart:core:package has Map class which defines following properties.
Properties | Explanation |
---|---|
Keys | It is used to get all keys as an iterable object. |
values | It is used to get all values as an iterable object. |
Length | It returns the length of the Map object. |
isEmpty | If the Map object contains no value, it returns true. |
isNotEmpty | If the Map object contains at least one value, it returns true. |
Example
void main() {
var student = new Map();
student['name'] = 'Tom';
student['age'] = 23;
student['course'] = 'B.tech';
student['Branch'] = 'Computer Science';
print(student);
// Get all Keys
print("The keys are : ${student.keys}");
// Get all values
print("The values are : ${student.values}");
// Length of Map
print("The length is : ${student.length}");
//isEmpty function
print(student.isEmpty);
//isNotEmpty function
print(student.isNotEmpty);
}
Output
{name: Tom, age: 23, course: B.tech, Branch: Computer Science}
The keys are : (name, age, course, Branch)
The values are : (Tom, 23, B.tech, Computer Science)
The length is : 4
false
true
Map Methods
The following lists the popular techniques.
AddAll(): This function adds several key-value pairs to another. Below is the syntax.
Syntax
Map.addAll(Map other)
Parameter:
- other: It indicates a pair of values. It yields a form of emptiness.
Example
void main() {
Map student = {'name':'Tom','age': 23};
print('Map :${student}');
student.addAll({'dept':'Civil','email':'tom@xyz.com'});
print('Map after adding key-values :${student}');
}
Output
Map :{name: Tom, age: 23}
Map after adding key-values :{name: Tom, age: 23, dept: Civil, email: tom@xyz.com}
remove() – It removes every pair from the map. Below is the syntax.
Syntax
Map.clear()
Example
void main() {
Map student = {'name':'Tom','age': 23};
print('Map :${student}');
student.clear();
print('Map after removing all key-values :${student}');
}
Output
Map :{name: Tom, age: 23}
Map after removing all key-values :{}
remove(): If the key exists in the specified map, it removes it together with its corresponding value. Below is the syntax.
Syntax
Map.remove(Object key)
Parameter –
- Keys: The specified entries are removed. It gives back the value linked to the given key.
Example
void main() {
Map student = {'name':'Tom','age': 23};
print('Map :${student}');
student.remove('age');
print('Map after removing given key :${student}');
}
Output
Map :{name: Tom, age: 23}
Map after removing given key :{name: Tom}
forEach() – The entries of the Map are iterated using it. Below is the syntax.
Syntax
Map.forEach(void f(K key, V value));
Parameter -
- f(K key, V value) - It denotes the key-value pair of the map.
Let's understand the following example.
Example -
Output:
Map :{name: Tom, age: 23}
name: Tom
age: 23