A collection of key/value pairs, from which you retrieve a value using its associated key.
There is a finite number of keys in the map, and each key has exactly one value associated with it.
Maps, and their keys and values, can be iterated. The order of iteration is defined by the individual type of map. Examples:
- The plain HashMap is unordered (no order is guaranteed),
- the LinkedHashMap iterates in key insertion order,
- a sorted map like SplayTreeMap iterates the keys in sorted order.
HashMap
The HashMap is unordered (the order of iteration is not guaranteed).
The keys of a HashMap must have consistent Object.== and Object.hashCode implementations. This means that the == operator must define a stable equivalence relation on the keys (reflexive, symmetric, transitive, and consistent over time), and that hashCode must be the same for objects that are considered equal by ==.
Maps are key-value pairs.
Keys must be unique. If you use the same key, you overwrite the old value.
Syntax –
var map={key1:value,key2:value,key3:value};
** using keys we can access values of map.
Let’s build a map of integers values.
Code 1:
void main(){ var IntMap={0:12,1:34,2:45,4:67}; print(IntMap); // print value of key -> 0 print(IntMap[0]); // print value of key -> 4 print(IntMap[4]); print(IntMap.runtimeType); } |

Let’s build a map of String values.
Code 2:
void main(){ var StrMap={1:’windows’,2:’linux’,3:’android’,4:’ios’}; print(‘String map is: $StrMap’); print(StrMap[1]); print(StrMap[3]); print(StrMap.runtimeType); } |

Another way to create map of specific types..
Let’s create a map pair of key(integers) & values(string)
Code 3:
import ‘dart:collection’; void main(){ final Map<int, String> os = HashMap(); // os[index(key)]=value os[0]=’android’; os[1]=’ios’; os[2]=’symbian’; print(os); } |

Let’s create a map pair of key(string) & values(double)
Code 4:
import ‘dart:collection’; void main(){ final Map<String,double> sal=HashMap(); sal[‘alex’]=20000.56; sal[‘anisha’]=45000.56; sal[‘pritha’]=67000.56; sal[‘trina’]=56000.56; sal[‘raja’]=58000.56; print(sal); // printing values as keys print(sal[‘alex’]); print(sal[‘raja’]); } |

Add new entries(key & value) into map.
For this we need to use .addAll() method. For getting map length we need to use .length.
Code 5:
void main(){ var StrMap={1:’windows’,2:’linux’,3:’android’}; print(‘String map is: $StrMap’); print(‘map length is: ‘); print(StrMap.length); // addAll() methpod -> {key:value} StrMap.addAll({4:’ios’}); print(‘map now: $StrMap’); print(‘map length is: ‘); print(StrMap.length); } |

The forEach iterates through all entries of a map. Let’s make a code, for prints all the key with values of a map.
Code 6:
void main(){ var StrMap={1:’windows’,2:’linux’,3:’android’}; print(‘String map is: $StrMap’); StrMap.forEach((key, value) { print(‘$key \t $value’); }); } |

Remove value by keys from map
.remove(key) method will delete value names as ‘key’. Let’s see below code…
Code 7:
void main(){ var StrMap={1:’windows’,2:’linux’,3:’android’,4:’ios’}; print(‘String map is: $StrMap’); // StrMap.remove(key); StrMap.remove(2); print(‘after deletion, String map is: $StrMap’); StrMap.remove(1); print(‘after deletion, String map is: $StrMap’); } |

Map contains
There have 2 methods & both returns boolean value(true/false) –
- .containsKey(key) method will remove value names with this key.
- .containsValue(‘value’) method will remove value from the map.
Let’s write a code, to see a key contains inside map or not.
Code 8:
void main(){ var StrMap={1:’windows’,2:’linux’,3:’android’,4:’ios’}; print(‘String map is: $StrMap’); // check contains key inside map ? print(StrMap.containsKey(2)); print(StrMap.containsKey(4)); print(StrMap.containsKey(7)); } |

Let’s write a code, to see a value contains inside map or not.
Code 9:
void main(){ var StrMap={1:’windows’,2:’linux’,3:’android’,4:’ios’}; print(‘String map is: $StrMap’); // check contains value inside map ? print(StrMap.containsValue(‘linux’)); print(StrMap.containsValue(‘arch linux’)); } |

Update map
We can update a single key-value or multiple key’s value in map.
- Map.update(key_name, (value) => ‘update_value’) => for a single key-value update.
- Map.updateAll((key, value) => ‘update_value’); => all key’s remains same but values got changed.
Write a code, to perform single key-value update in map.
Code 10:
void main(){ var StrMap={1:’windows xp’,2:’linux’,3:’android’,4:’ios’}; print(‘String map is: $StrMap’); StrMap.update(1, (value) => ‘windows 11’); print(‘Map Now: $StrMap’); StrMap.update(2, (value) => ‘kali linux’); print(‘Map Now: $StrMap’); } |

Write a code, to perform multi key’s value update in map.
Code 11:
void main(){ var StrMap={1:’google’,2:’apple’,3:’microcodes’,4:’github’}; print(‘String map is: $StrMap’); // update multiple key’s value StrMap.updateAll((key, value) => ‘microsoft’); print(‘String map is: $StrMap’); } |

Map clear
You can clear a map all keys and values using .clear.
Code 12:
void main(){ var StrMap={1:’google’,2:’apple’,3:’microcodes’,4:’github’}; print(‘String map is: $StrMap’); // .isEmpty -> checks a map empty or not. print(StrMap.isEmpty); // prints map length/size print(StrMap.length); StrMap.clear(); print(StrMap.isEmpty); print(StrMap.length); } |

Write a code, to check a value contains inside a map or not.
Code 13:
import ‘dart:io’; void main(){ late String? name; var StrMap={1:’google’,2:’apple’,3:’microcodes’,4:’github’}; print(‘String map is: $StrMap’); print(“Enter value u find: “); name=stdin.readLineSync(); if(StrMap.containsValue(name)){ print(‘value found!’); } else{ print(‘value not found!’); } } |
