Maps: the Key, Value Pair in Dart Programming Language

https://leanpub.com/u/sanjibsinha

laern-dart-the-hard-way
https://leanpub.com/learndartthehardway An unordered collection of Key-Value pair is known as Map in Dart. The main advantage of Map is the Key-Value pair can be of any type. To begin with, let us start with some points that we should remember while working with Map.
1. Each Key in a Map should be unique. 2. The value can be repeated. 3. The Map can commonly be called hash or dictionary. 4. Size of a Map is not fixed, it can either increase or decrease as per the number of elements. In other words, Maps can grow or shrink at runtime. 5. HashMap is an implementation of a Map and it is based on a Hash table.
Let us see a code snippet to understand how a Map works in Dart. //code 9.4 void mapFunction(){ //unordered collection of key=>value pair Map countries = Map(); countries['India'] = "Asia"; countries["German"] = "Europe"; countries["France"] = "Europe"; countries["Brazil"] = "South America"; //1. method we can obtain key or value for(var key in countries.keys){ print("Countries' name: $key"); } print("-----------"); for(String value in countries.values){ print("Continents' name: $value"); } //2. method countries.forEach((key, value) => print("Country: $key and Continent: $value")); //we can update any map very easily if(countries.containsKey("German")){ countries.update("German", (value) => "European Union"); print("Updated country German."); countries.forEach((key, value) => print("Country: $key and Continent: $value")); } //we can remove any country countries.remove("Brazil"); countries.forEach((key, value) => print("Country: $key and Continent: $value")); print("Barzil has been removed successfully."); print("-----------"); //3. method of creating a map Map telephoneNumbersOfCustomers = { "John" : 1234, "Mac" : 7534, "Molly" : 8934, "Plywod" : 1275, "Hagudu" : 2534 }; telephoneNumbersOfCustomers.forEach((key, value) => print("Customer: $key and Contact NUmber: $value")); } main(List arguments){ mapFunction(); } And here is the output of code 9.4 Countries' name: India Countries' name: German Countries' name: France Countries' name: Brazil ----------- Continents' name: Asia Continents' name: Europe Continents' name: Europe Continents' name: South America Country: India and Continent: Asia Country: German and Continent: Europe Country: France and Continent: Europe Country: Brazil and Continent: South America Updated country German. Country: India and Continent: Asia Country: German and Continent: European Union Country: France and Continent: Europe Country: Brazil and Continent: South America Country: India and Continent: Asia Country: German and Continent: European Union Country: France and Continent: Europe Barzil has been removed successfully. ----------- Customer: John and Contact NUmber: 1234 Customer: Mac and Contact NUmber: 7534 Customer: Molly and Contact NUmber: 8934 Customer: Plywod and Contact NUmber: 1275 Customer: Hagudu and Contact NUmber: 2534 There are three methods that we use to retrieve the values of a Map. //1. method we can obtain key or value for(var key in countries.keys){ print("Countries' name: $key"); } print("-----------"); //2. Method for(String value in countries.values){ print("Continents' name: $value"); } //3. method countries.forEach((key, value) => print("Country: $key and Continent: $value")); Besides, there are several methods to add, update or remove the elements in a Map. As we progress and build apps in native iOS or Android, we will see more features of Map. Lastly we will see another collection feature in Map, which is called Queue.