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,
- 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. - We declare an empty map named
myMap
to store the key-value pairs. - We iterate over the list using the
forEach
method and add each pair to the map. - 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,
- 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. - We declare an empty map named
myMap
to store the key-value pairs. - We iterate over the list using the
forEach
method and add each tuple to the map. - 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,
- 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. - We declare an empty map named
finalMap
to store the combined key-value pairs. - We iterate over the list using the
forEach
method and add each key-value pair from each map to the final map. - 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
- How to create an Empty Map in Dart ?
- How to create a Map with Initial Key-Value Pairs in Dart ?
- How to Print a Map in Dart ?
- How to Add a Key-Value Pair to a Map in Dart ?
- How to Set a Default Value for a Key in a Map in Dart ?
- How to Update the Value for a Key in a Map in Dart ?
- How to Check if a Map is Empty in Dart ?
- How to Check if a Key Exists in a Map in Dart ?
- How to Check if a Value Exists in a Map in Dart ?
- How to Get the Value Associated with a Key in a Map in Dart ?
- How to Remove a Key-Value Pair from a Map in Dart ?
- How to Remove Key-Value Pairs from a Map Based on Values in Dart ?
- How to Clear All Key-Value Pairs from a Map in Dart ?
- How to Iterate Over Keys in a Map in Dart ?
- How to Iterate Over Values in a Map in Dart ?
- How to Iterate Over Entries (Key-Value Pairs) in a Map in Dart ?
- How to Get the Size (Number of Key-Value Pairs) of a Map in Dart ?
- How to Convert a Map to an Array of Keys in Dart ?
- How to Convert a Map to an Array of Values in Dart ?
- How to Convert a Map to an Array of Key-Value Pairs in Dart ?
- How to Merge Two Maps in Dart ?
- How to Check if Two Maps are Equal in Dart ?
- How to Sort a Map by Keys in Dart ?
- How to Sort a Map by Values in Dart ?
- How to Filter a Map Based on Keys in Dart ?
- How to Filter a Map Based on Values in Dart ?
- How to Reduce Values in a Map to a Single Value in Dart ?
- How to Convert an Array of Key-Value Pairs to a Map in Dart ?
- How to Convert a Map to a JSON String in Dart ?
- How to Convert a JSON String to a Map in Dart ?
- How to Swap Keys and Values in a Map in Dart ?
- How to Create a Map of Maps in Dart ?
- How to Iterate Over a Map of Maps in Dart ?