Dart Map is an object that stores data in the form of a key-value pair. Each value is associated with its key, and it is used to access its corresponding value. Both keys and values can be any type. In Dart Map, each key must be unique, but the same value can occur multiple times. The Map representation is quite similar to Python Dictionary. The Map can be declared by using curly braces {} ,and each key-value pair is separated by the commas(,). The value of the key can be accessed by using a square bracket([]).
Declaring a Dart Map
Dart Map can be defined in two methods.
- Using Map Literal
- Using Map Constructor
The syntax of declaring Dart Map is given below.
Using Map Literals
To declare a Map using map literal, the key-value pairs are enclosed within the curly braces “{}” and separated by the commas. The syntax is given below.
Syntax –
var map_name = {key1:value1, key2:value2 [.......,key_n: value_n]}
Example – 1:
void main() {
var student = {'name':'Tom','age':'23'};
print(student);
}
Output:
{name: Tom, age: 23}
Example – 2: Adding value at runtime
void main() {
var student = {'name':' tom', 'age':23};
student['course'] = 'B.tech';
print(student);
}
Output:
{name: tom, age: 23, course: B.tech}
Explanation –
In the above example, we declared a Map of a student name. We added the value at runtime by using a square bracket and passed the new key as a course associated with its value.
Using Map Constructor
To declare the Dart Map using map constructor can be done in two ways. First, declare a map using map() constructor. Second, initialize the map. The syntax is given below.
Syntax –
var map_name = new map()
After that, initialize the values.
map_name[key] = value
Example – 1: Map constructor
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}
Note – A map value can be any object including NULL.
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 commonly used methods are given below.
addAll() – It adds multiple key-value pairs of other. The syntax is given below.
Syntax –
Map.addAll(Map<Key, Value> other)
Parameter:
- other – It denotes a key-value pair. It returns a void type.
Let’s understand the following example.
Example –
void main() {
Map student = {'name':'Tom','age': 23};
print('Map :${student}');
student.addAll({'dept':'Civil','email':'[email protected]'});
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: [email protected]}
remove() – It eliminates all pairs from the map. The syntax is given below.
Syntax –
Map.clear()
Let’s have a look at following example.
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() – It removes the key and its associated value if it exists in the given map. The syntax is given below.
Syntax –
Map.remove(Object key)
Parameter –
- Keys – It deletes the given entries. It returns the value associated with the specified key.
Let’s understand the following example.
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() – It is used to iterate the Map’s entries. The syntax is given below.
Syntax –
Map.forEach(void f(K key, V value));
</pre></div>
<p><strong>Parameter -</strong></p>
<ul class="points">
<li><strong>f(K key, V value) -</strong> It denotes the key-value pair of the map.</li>
</ul>
<p>Let's understand the following example.</p>
<p><strong>Example -</strong></p>
<div class="codeblock"><textarea name="code" class="java">
void main() {
Map student = {'name':'Tom','age': 23};
print('Map :${student}');
student.forEach((k,v) => print('${k}: ${v}'));
}
Output:
Map :{name: Tom, age: 23}
name: Tom
age: 23
Leave a Reply