Dart Map

 

Dart Map

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 -

  1. var map_name = {key1:value1, key2:value2 [.......,key_n: value_n]}  

Example - 1:

  1. void main() {   
  2.    var student = {'name':'Tom','age':'23'};   
  3.    print(student);   
  4. }  

Output:

{name: Tom, age: 23}

Example - 2: Adding value at runtime

  1. void main() {   
  2.    var student = {'name':' tom''age':23};   
  3.    student['course'] = 'B.tech';   
  4.    print(student);   
  5. }  

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 -

  1. var map_name = new map()  

After that, initialize the values.

  1. map_name[key] = value  

Example - 1: Map constructor

  1. void main() {   
  2.    var student = new Map();   
  3.    student['name'] = 'Tom';   
  4.    student['age'] = 23;   
  5.    student['course'] = 'B.tech';   
  6.    student['Branch'] = 'Computer Science';  
  7.    print(student);   
  8. }  

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.

PropertiesExplanation
KeysIt is used to get all keys as an iterable object.
valuesIt is used to get all values as an iterable object.
LengthIt returns the length of the Map object.
isEmptyIf the Map object contains no value, it returns true.
isNotEmptyIf the Map object contains at least one value, it returns true.

Example -

  1. void main() {   
  2.    var student = new Map();   
  3.    student['name'] = 'Tom';   
  4.    student['age'] = 23;   
  5.    student['course'] = 'B.tech';   
  6.    student['Branch'] = 'Computer Science';  
  7.    print(student);   
  8.   
  9.   // Get all Keys  
  10.   print("The keys are : ${student.keys}");  
  11.   
  12.  // Get all values  
  13.  print("The values are : ${student.values}");  
  14.    
  15.  // Length of Map  
  16.  print("The length is : ${student.length}");  
  17.   
  18. //isEmpty function  
  19. print(student.isEmpty);  
  20.   
  21. //isNotEmpty function  
  22. print(student.isNotEmpty);  
  23. }  

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 -

  1. 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 -

  1. void main() {   
  2.    Map student = {'name':'Tom','age'23};   
  3.    print('Map :${student}');   
  4.      
  5.    student.addAll({'dept':'Civil','email':'tom@xyz.com'});   
  6.    print('Map after adding  key-values :${student}');   
  7. }  

Output:

Map :{name: Tom, age: 23}
Map after adding  key-values :{name: Tom, age: 23, dept: Civil, email: tom@xyz.com}

remove() - It eliminates all pairs from the map. The syntax is given below.

Syntax -

  1. Map.clear()  

Let's have a look at following example.

Example -

  1. void main() {   
  2.    Map student = {'name':'Tom','age'23};   
  3.    print('Map :${student}');   
  4.      
  5.    student.clear();   
  6.    print('Map after removing all key-values :${student}');   
  7.     
  8. }  

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 -

  1. 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 -

  1. void main() {   
  2.    Map student = {'name':'Tom','age'23};   
  3.    print('Map :${student}');   
  4.      
  5.    student.remove('age');   
  6.    print('Map after removing given key :${student}');   
  7. }  

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 -

  1. Map.forEach(void f(K key, V value));  
  2. </pre></div>  
  3. <p><strong>Parameter -</strong></p>  
  4. <ul class="points">  
  5. <li><strong>f(K key, V value) -</strong> It denotes the key-value pair of the map.</li>  
  6. </ul>  
  7. <p>Let's understand the following example.</p>  
  8. <p><strong>Example -</strong></p>  
  9. <div class="codeblock"><textarea name="code" class="java">  
  10. void main() {   
  11.    Map student = {'name':'Tom','age'23};   
  12.    print('Map :${student}');   
  13.    student.forEach((k,v) => print('${k}: ${v}'));   
  14.      
  15. }  

Output:

Map :{name: Tom, age: 23}
name: Tom
age: 23

Comments