Education
Software Development Executive - I
Software Development Executive - II
Last updated on Oct 26, 2023
Last updated on Oct 4, 2023
In programming, data structures play a vital role in organizing and storing data. One such data structure that has proven to be incredibly useful is the Map. In Dart programming language, the Map data structure is a collection of key-value pairs, where each key has exactly one value associated with it.
This blog post delves deep into the technical aspects of Dart Map, exploring its various features, methods, properties, and how we can effectively use it in our Dart programs. From understanding the concept of key-value pairs to creating and manipulating maps, we will cover it all.
A Dart Map, often simply referred to as a 'map', is a collection of key-value pairs. In a map, there is a finite number of keys, and each key has exactly one value associated with it. This key-value pair system allows us to retrieve a value using its associated key.
1 // Creating a new map in Dart 2 Map<String, int> map = { 3 'apple': 5, 4 'banana': 7, 5 'orange': 8 6 }; 7
In the above code snippet, we have created a new map where the keys are strings (fruit names) and the values are integers (quantities).
Dart map supports a variety of operations, including adding, updating, and removing key-value pairs. It also supports various methods to manipulate and interact with the map. For instance, we can use the forEach method to apply a function to each key-value pair in the map, or the containsKey method to check if a given key exists in the map.
1 // Using forEach method in Dart map 2 map.forEach((key, value) { 3 print('$key: $value'); 4 }); 5 6 // Using containsKey method in Dart map 7 if (map.containsKey('apple')) { 8 print('Apple exists in the map'); 9 } else { 10 print('Apple does not exist in the map'); 11 } 12
In the first code snippet, we are using the forEach method to print all key-value pairs in the map. In the second code snippet, we are using the containsKey method to check if 'apple' exists as a key in the map.
The order of iteration in a map is defined by the individual type of map. For example, a HashMap is unordered (no order is guaranteed), a LinkedHashMap iterates in key insertion order, and a sorted map like SplayTreeMap iterates the keys in sorted order.
In a Dart map, data is stored in the form of key-value pairs. Each key in the map is associated with exactly one value, forming a key-value pair. The key acts as an identifier for the value, and it can be used to retrieve the associated value at any time.
1 // Accessing value using key in Dart map 2 int appleQuantity = map['apple']; 3 print('Quantity of apple: $appleQuantity'); 4
In the above code snippet, we are using the key 'apple' to retrieve its associated value from the map.
In Dart, the type of both keys and values in a map can be explicitly defined. This is known as a key-value type map. For instance, we can have a map where the keys are of type String and the values are of type int.
1 // Defining key-value type map in Dart 2 Map<String, int> map = { 3 'apple': 5, 4 'banana': 7, 5 'orange': 8 6 }; 7
In the above code snippet, we have defined a map where the keys are strings (fruit names) and the values are integers (quantities). This helps in ensuring that the map only contains the desired type of keys and values, thereby reducing the chances of runtime errors.
Dart provides several constructors to create a new map. The Map() constructor is used to create an empty map. We can also use the Map.from() constructor to create a new map with the same keys and values as another map.
1 // Creating an empty map using Map() constructor 2 Map<String, int> map1 = Map(); 3 4 // Creating a new map with the same keys and values as another map 5 Map<String, int> map2 = Map.from(map1); 6
In the above code snippet, we first create an empty map using the Map() constructor. Then, we create a new map with the same keys and values as map1 using the Map.from() constructor.
Map literals are another way to create a new map in Dart. We can define a map literal by enclosing a comma-separated list of key-value pairs in curly braces .
1 // Creating a new map using map literals 2 Map<String, int> map = { 3 'apple': 5, 4 'banana': 7, 5 'orange': 8 6 }; 7
In the above code snippet, we create a new map using map literals. The keys are strings (fruit names) and the values are integers (quantities).
In Dart, we can also initialize a map at the time of its creation. This can be done using the Map.fromIterables() constructor, which creates a map associating the given keys to the given values.
1 // Initializing a map in Dart 2 List<String> keys = ['apple', 'banana', 'orange']; 3 List<int> values = [5, 7, 8]; 4 5 Map<String, int> map = Map.fromIterables(keys, values); 6
In the above code snippet, we first define two lists keys and values. Then, we initialize a map with these keys and values using the Map.fromIterables() constructor.
In Dart, we can access the value associated with a specific key in a map using the square brackets [] notation. If the map contains the given key, the associated value is returned. If the map does not contain the key, null is returned.
1 // Accessing map values in Dart 2 int appleQuantity = map['apple']; 3 print('Quantity of apple: $appleQuantity'); 4
In the above code snippet, we are accessing the value associated with the key 'apple' in the map.
We can update the value associated with a specific key in a map using the square brackets [] notation. If the map already contains the key, the existing value is overwritten. If the map does not contain the key, a new key-value pair is added to the map.
1 // Updating map values in Dart 2 map['apple'] = 10; 3 print('Updated quantity of apple: ${map['apple']}'); 4
In the above code snippet, we are updating the value associated with the key 'apple' in the map.
We can remove a key-value pair from a map using the remove() method. This method removes the key and its associated value from the map. If the map contains the key, the removed value is returned. If the map does not contain the key, null is returned.
1 // Removing map values in Dart 2 int removedValue = map.remove('apple'); 3 print('Removed value: $removedValue'); 4
In the above code snippet, we are removing the key-value pair with the key 'apple' from the map.
Dart Map provides several methods that operate on a given value.
For instance, the Map.castFrom() method adapts the source map to be a map with a different type of keys and values.
1 // Using Map.castFrom() static method 2 Map<String, int> map1 = { 3 'apple': 5, 4 'banana': 7, 5 'orange': 8 6 }; 7 8 Map<String, double> map2 = Map.castFrom(map1); 9
In the above code snippet, we are using the Map.castFrom() static method to create a new map map2 with the same keys as map1 but with values of type double.
Dart Map provides several properties that can be used to interact with the map.
For instance, the length property returns the number of key-value pairs in the map, and the keys property returns an iterable of all keys in the map.
1 // Using length and keys properties in Dart map 2 int mapLength = map.length; 3 Iterable<String> mapKeys = map.keys; 4 5 print('Length of map: $mapLength'); 6 print('Keys in map: $mapKeys'); 7
In the above code snippet, we are using the length and keys properties to get the number of key-value pairs and all keys in the map, respectively.
We can create an instance of a map in Dart using the Map() constructor or any other map constructor. Once the map instance is created, we can use various map methods and properties to manipulate and interact with the map.
1 // Creating a map instance in Dart 2 Map<String, int> map = Map(); 3 4 // Adding key-value pairs to the map 5 map['apple'] = 5; 6 map['banana'] = 7; 7 map['orange'] = 8; 8
In the above code snippet, we first create a map instance using the Map() constructor. Then, we add key-value pairs to the map using the square brackets [] notation.
In this blog post, we delved deep into the technical aspects of Dart Map, a powerful and versatile data structure in Dart. We explored how Dart Map is a collection of key-value pairs, where each key has exactly one value associated with it. We also looked at how to create and initialize maps, access, update, and remove map values, and the various methods and properties provided by Dart Map.
Understanding Dart Map and its functionalities is crucial for any Dart programmer, as it is a fundamental data structure used in many programming scenarios. With its ability to store data in key-value pairs, Dart Map provides an efficient way to organize and manipulate data.
We hope this blog post has provided you with a comprehensive understanding of Dart Map and its functionalities.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.
Tired coding all day?
Do it with a few clicks.