Maps in Dart Programming

Dart ProgrammingServer Side ProgrammingProgramming

Maps are very important data structures as they allow us to map keys to some specific values, and later we can get the values from the keys.

In Dart, we have different types of maps available to us. These mainly are −

  • HashMap

  • LinkedHashMap

  • SplayTreeMap

In most cases, we make use of LinkedHashMap as it is very easy to create and make use of.

Let's create a simple map in dart.

Example

Consider the example shown below −

 Live Demo

void main() {
   var colors = new Map();
   print(colors);
}

In the above example, we created an empty map and then printed it out. It should be noted that when we create a map with a Map() constructor it will create a LinkedHashMap.

A LinkedHashMap is different from a HashMap in a way that it preserves the order in which we insert the keys into it.

Output

{}

Now, let's try to add some key-value pairs into our colors map.

Example

Consider the example shown below −

 Live Demo

void main() {
   var colors = new Map();
   colors['blue'] = true;
   colors['red'] = false;
   colors['green'] = false;
   colors['yellow'] = true;
   print(colors);
}

The keys are provided inside the square brackets and the values that we want to assign to those keys are present on the right side of the expression.

Output

{blue: true, red: false, green: false, yellow: true}

It should be noted that the order in which we inserted the keys is maintained when we print the map. Also, it is not important that all the keys must be of the same data type in the map.

Example

Consider the example shown below −

 Live Demo

void main() {
   var colors = new Map();
   colors['blue'] = true;
   colors['red'] = false;
   colors['green'] = false;
   colors['yellow'] = true;
   colors[1] = "omg"; // int key with string value
   print(colors);
}

In Dart, it is perfectly fine to have dynamic keys and values.

Output

{blue: true, red: false, green: false, yellow: true, 1: omg}

Now let's look at some of the properties that we can use on a map.

Example

Consider the example shown below −

 Live Demo

void main() {
   var colors = new Map();
   colors['blue'] = true;
   colors['red'] = false;
   colors['green'] = false;
   colors['yellow'] = true;
   colors[1] = "omg";
   print(colors['blue']); // accessing a specific key
   print(colors.length); // checking the number of key-value pairs present in the map
   print(colors.isEmpty); // checking if the map is empty or not
   print(colors.keys); // printing all the keys present in the map
   print(colors);
}

Output

true
5
false
(blue, red, green, yellow, 1)
{blue: true, red: false, green: false, yellow: true, 1: omg}

We can also iterate over the keys and values that are present in the map by using the for-in loop.

Example

Consider the example shown below −

 Live Demo

void main() {
   var colors = new Map();
   colors['blue'] = true;
   colors['red'] = false;
   colors['green'] = false;
   colors['yellow'] = true;
   colors[1] = "omg";void main() {
      var colors = new Map();
      colors['blue'] = true;
      colors['red'] = false;
      colors['green'] = false;
      colors['yellow'] = true;
      colors[1] = "omg";
      print(" ---- Keys ---- ");
      for(var key in colors.keys){
         print(key);
      }
      print(" ---- Values ---- ");
      for(var value in colors.values){
         print(value);
      }
   }
   print(" ---- Keys ---- ");
   for(var key in colors.keys){
      print(key);
   }
   print(" ---- Values ---- ");
   for(var value in colors.values){
      print(value);
   }
}

Output

---- Keys ----
blue
red
green
yellow
1
---- Values ----
true
false
false
true
omg
raja
Published on 21-May-2021 13:08:12
Advertisements