This tutorial shows you various ways to merge multiple Map
s in Dart.
Map
is a data type that consists of key-value pairs. It's a suitable data type if you often need to find an object with a certain identifier. If you have several Map
s and you want to combine it into one, there are several ways to do it in Dart. Below are the examples.
In the examples, we are going to combine the following Map
s.
var a = {
'one': '1',
'two': '2',
'three': '3',
};
var b = {
'four': '4',
'five': '5',
'six': '6',
};
var c = {
'six': '6a',
'seven': '7',
};
Using addAll
The Map
class has an addAll
method, which adds the entries of another Map
. To merge multiple Map
s, create an empty one first, then call addAll
for each Map
to be merged.
var result = {};
result.addAll(a);
result.addAll(b);
result.addAll(c);
print(result);
Output:
{one: 1, two: 2, three: 3, four: 4, five: 5, six: 6a, seven: 7}
It's also possible to chain multiple addAll
operators using ..
(double dot).
result
..addAll(a)
..addAll(b)
..addAll(c);
print(result);
Using Map.fromEntries
Map
has a factory method called fromEntries
which can be used to create a Map
from an Iterable
of MapEntry
objects. To use it, you need to convert the Map
s to MapEntry
objects. It can be done by combining them to a List
first, then get all the entries using the expand
operator.
var result = Map.fromEntries([a, b, c].expand((map) => map.entries));
print(result);
Output:
{one: 1, two: 2, three: 3, four: 4, five: 5, six: 6a, seven: 7}
Using Spread Operator
If you use Dart 2.3 or newer, you can use spread operator (triple dot) as shown in the example below.
var result = {
...a,
...b,
...c,
};
print(result);
Output:
{one: 1, two: 2, three: 3, four: 4, five: 5, six: 6a, seven: 7}
Using CombinedMapView
An easy way to combine two or more Map
s is by using CombinedMapView
. It's a class of Dart collection
package, which is an official package by the Dart team. To use it, you can run dart pub add collection
(or flutter pub add collection
for a Flutter project) to install it.
In your code, you need to import package:collection/collection.dart
. Then, you can call the constructor of CombinedMapView
by passing the Map
s to be merged as a List
.
var result = CombinedMapView([a, b, c]);
print(result);
Output:
{one: 1, two: 2, three: 3, four: 4, five: 5, six: 6a, seven: 7}
Handle Duplicate Keys
Using the previous methods, if there is any duplicate key, the latter one is going to replace the earlier ones. What if you want to use a different logic? For example, you want to use the first key or compare the values to select which one to use. A possible solution is by using for loops to iterate each entries of each Map
s. The example below uses putIfAbsent
method which only adds the key if it's not already exist.
var result = {};
for (var map in [a, b, c]) {
for (var entry in map.entries) {
result.putIfAbsent(entry.key, () => entry.value);
}
}
print(result);
Output:
{one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7}
Summary
This tutorial explains several ways for merging two or more Map
s. Just choose the one that you prefer and suits your case.
You can also read about: