How to Convert an Array of Key-Value Pairs to a Map in Dart - Step by Step Examples



How to Convert an Array of Key-Value Pairs to a Map in Dart ?

Answer

To convert an array of key-value pairs to a map in Dart, you can iterate over the array and insert each key-value pair into the map. This method allows you to create a map from a collection of key-value pairs stored in an array.



✐ Examples

1 Converting a List of Pairs to a Map

We can convert a list of key-value pairs to a map in Dart by iterating over the list and adding each key-value pair to the map.

For example,

  1. We start by declaring and initializing a list of pairs named pairs with some key-value pairs. In this example, the pairs are represented as lists with two elements: a key and a value.
  2. We declare an empty map named myMap to store the key-value pairs.
  3. We iterate over the list using the forEach method and add each pair to the map.
  4. We print the resulting map to the console using the print function to verify the conversion.

Dart Program

void main() {
    // Declare and initialize a list of pairs
    List<List<int>> pairs = [
        [1, 10],
        [2, 20],
        [3, 30],
        [4, 40],
        [5, 50]
    ];

    // Declare an empty map to store the key-value pairs
    Map<int, int> myMap = {};

    // Iterate over the list and add each pair to the map
    pairs.forEach((pair) {
        myMap[pair[0]] = pair[1];
    });

    // Print the resulting map
    print('Map from list of pairs:');
    myMap.forEach((key, value) {
        print('$key: $value');
    });
}

Output

Map from list of pairs:
1: 10
2: 20
3: 30
4: 40
5: 50

2 Converting a List of Tuples to a Map

We can convert a list of key-value tuples to a map in Dart by iterating over the list and adding each tuple's key-value pair to the map.

For example,

  1. We start by declaring and initializing a list of tuples named pairs with some key-value pairs. In this example, the tuples are represented as lists with two elements: a key and a value.
  2. We declare an empty map named myMap to store the key-value pairs.
  3. We iterate over the list using the forEach method and add each tuple to the map.
  4. We print the resulting map to the console using the print function to verify the conversion.

Dart Program

void main() {
    // Declare and initialize a list of tuples
    List<Tuple<int, int>> pairs = [
        Tuple(1, 10),
        Tuple(2, 20),
        Tuple(3, 30),
        Tuple(4, 40),
        Tuple(5, 50)
    ];

    // Declare an empty map to store the key-value pairs
    Map<int, int> myMap = {};

    // Iterate over the list and add each tuple to the map
    pairs.forEach((tuple) {
        myMap[tuple.item1] = tuple.item2;
    });

    // Print the resulting map
    print('Map from list of tuples:');
    myMap.forEach((key, value) {
        print('$key: $value');
    });
}

// Define a tuple class
class Tuple<T1, T2> {
    final T1 item1;
    final T2 item2;

    Tuple(this.item1, this.item2);
}

Output

Map from list of tuples:
1: 10
2: 20
3: 30
4: 40
5: 50

3 Converting a List of Maps to a Single Map

We can convert a list of maps to a single map in Dart by iterating over the list and adding each key-value pair from each map to the final map.

For example,

  1. We start by declaring and initializing a list of maps named listOfMaps with some key-value pairs. Each map in the list represents a key-value pair.
  2. We declare an empty map named finalMap to store the combined key-value pairs.
  3. We iterate over the list using the forEach method and add each key-value pair from each map to the final map.
  4. We print the resulting map to the console using the print function to verify the conversion.

Dart Program

void main() {
    // Declare and initialize a list of maps
    List<Map<int, int>> listOfMaps = [
        {1: 10},
        {2: 20},
        {3: 30},
        {4: 40},
        {5: 50}
    ];

    // Declare an empty map to store the combined key-value pairs
    Map<int, int> finalMap = {};

    // Iterate over the list and add each key-value pair to the final map
    listOfMaps.forEach((map) {
        map.forEach((key, value) {
            finalMap[key] = value;
        });
    });

    // Print the resulting map
    print('Map from list of maps:');
    finalMap.forEach((key, value) {
        print('$key: $value');
    });
}

Output

Map from list of maps:
1: 10
2: 20
3: 30
4: 40
5: 50

Summary

In this tutorial, we learned How to Convert an Array of Key-Value Pairs to a Map in Dart language with well detailed examples.




More Dart Maps Tutorials

  1. How to create an Empty Map in Dart ?
  2. How to create a Map with Initial Key-Value Pairs in Dart ?
  3. How to Print a Map in Dart ?
  4. How to Add a Key-Value Pair to a Map in Dart ?
  5. How to Set a Default Value for a Key in a Map in Dart ?
  6. How to Update the Value for a Key in a Map in Dart ?
  7. How to Check if a Map is Empty in Dart ?
  8. How to Check if a Key Exists in a Map in Dart ?
  9. How to Check if a Value Exists in a Map in Dart ?
  10. How to Get the Value Associated with a Key in a Map in Dart ?
  11. How to Remove a Key-Value Pair from a Map in Dart ?
  12. How to Remove Key-Value Pairs from a Map Based on Values in Dart ?
  13. How to Clear All Key-Value Pairs from a Map in Dart ?
  14. How to Iterate Over Keys in a Map in Dart ?
  15. How to Iterate Over Values in a Map in Dart ?
  16. How to Iterate Over Entries (Key-Value Pairs) in a Map in Dart ?
  17. How to Get the Size (Number of Key-Value Pairs) of a Map in Dart ?
  18. How to Convert a Map to an Array of Keys in Dart ?
  19. How to Convert a Map to an Array of Values in Dart ?
  20. How to Convert a Map to an Array of Key-Value Pairs in Dart ?
  21. How to Merge Two Maps in Dart ?
  22. How to Check if Two Maps are Equal in Dart ?
  23. How to Sort a Map by Keys in Dart ?
  24. How to Sort a Map by Values in Dart ?
  25. How to Filter a Map Based on Keys in Dart ?
  26. How to Filter a Map Based on Values in Dart ?
  27. How to Reduce Values in a Map to a Single Value in Dart ?
  28. How to Convert an Array of Key-Value Pairs to a Map in Dart ?
  29. How to Convert a Map to a JSON String in Dart ?
  30. How to Convert a JSON String to a Map in Dart ?
  31. How to Swap Keys and Values in a Map in Dart ?
  32. How to Create a Map of Maps in Dart ?
  33. How to Iterate Over a Map of Maps in Dart ?