Dart Map


In Dart, a map is a collection of key-value pairs, where each key is unique and is used to access its corresponding value. Maps are highly useful for associating related data, allowing for efficient lookups, insertions, and deletions based on keys. Dart provides a built-in Map class with various methods and properties to facilitate working with maps.

Creating Maps

You can create maps in Dart using map literals or the Map constructor.

  1. Using Map Literals:
void main() { // Create a map using literal notation Map<String, int> ages = { 'Alice': 30, 'Bob': 25, 'Charlie': 35, }; print(ages); // Output: {Alice: 30, Bob: 25, Charlie: 35} }
  1. Using the Map Constructor:

You can also create an empty map or initialize a map with key-value pairs using the Map constructor.

void main() { // Create an empty map Map<String, int> emptyMap = Map<String, int>(); // Create a map with key-value pairs Map<String, int> initializedMap = Map.from({'Alice': 30, 'Bob': 25}); print(emptyMap); // Output: {} print(initializedMap); // Output: {Alice: 30, Bob: 25} }

Accessing Map Elements

You can access values in a map using their corresponding keys.

Example:

void main() { Map<String, int> ages = { 'Alice': 30, 'Bob': 25, 'Charlie': 35, }; print(ages['Alice']); // Output: 30 print(ages['Bob']); // Output: 25 }

Modifying Maps

Maps are mutable, which means you can change their contents after creation:

  • Adding Key-Value Pairs: You can add new entries by assigning a value to a new key.
void main() { Map<String, int> ages = { 'Alice': 30, 'Bob': 25, }; // Add a new key-value pair ages['Charlie'] = 35; print(ages); // Output: {Alice: 30, Bob: 25, Charlie: 35} }
  • Updating Values: You can update the value associated with an existing key.
void main() { Map<String, int> ages = { 'Alice': 30, 'Bob': 25, }; // Update an existing value ages['Bob'] = 26; print(ages); // Output: {Alice: 30, Bob: 26} }
  • Removing Key-Value Pairs: Use the remove() method to delete a key-value pair.
void main() { Map<String, int> ages = { 'Alice': 30, 'Bob': 25, }; // Remove a key-value pair ages.remove('Alice'); print(ages); // Output: {Bob: 25} }

Map Properties and Methods

Dart provides various properties and methods to work with maps:

  • Keys: Get a list of all keys in the map.
void main() { Map<String, int> ages = { 'Alice': 30, 'Bob': 25, }; print(ages.keys); // Output: (Alice, Bob) }
  • Values: Get a list of all values in the map.
void main() { Map<String, int> ages = { 'Alice': 30, 'Bob': 25, }; print(ages.values); // Output: (30, 25) }
  • Contains Key: Check if a key exists in the map.
void main() { Map<String, int> ages = { 'Alice': 30, 'Bob': 25, }; print(ages.containsKey('Alice')); // Output: true print(ages.containsKey('Charlie')); // Output: false }
  • Contains Value: Check if a value exists in the map.
void main() { Map<String, int> ages = { 'Alice': 30, 'Bob': 25, }; print(ages.containsValue(30)); // Output: true print(ages.containsValue(40)); // Output: false }
  • Clear the Map: Remove all key-value pairs from the map.
void main() { Map<String, int> ages = { 'Alice': 30, 'Bob': 25, }; ages.clear(); print(ages); // Output: {} }

Iterating Over Maps

You can iterate over the keys or key-value pairs in a map using various methods:

  1. Using a for Loop:
void main() { Map<String, int> ages = { 'Alice': 30, 'Bob': 25, }; for (String name in ages.keys) { print('$name is ${ages[name]} years old'); } }
  1. Using the forEach Method:
void main() { Map<String, int> ages = { 'Alice': 30, 'Bob': 25, }; ages.forEach((name, age) { print('$name is $age years old'); }); }

Conclusion

Maps in Dart are a powerful data structure for associating keys with values, allowing for efficient data retrieval and manipulation. Their unique keys make them suitable for a variety of applications, from representing JSON-like data structures to implementing dictionaries and lookups. By understanding how to work with maps, including adding, updating, removing, and iterating over elements, you can enhance the functionality of your Dart applications and write more efficient and organized code. Mastering maps is essential for any Dart developer, as they play a crucial role in managing data effectively.