Dart Map
Dart Map
ZetCode
All Spring Boot Python C# Java JavaScript Subscribe
Dart Map
last modified December 11, 2020
Dart Map tutorial shows how to work with a map collection in Dart language.
A map is a collection of key/value pairs. The value is retrieved from a map with its
associated key. Maps are also called dictionaries, associative arrays, or hash tables.
Depending on the iteration order, there are three types of maps in Dart:
HashMap - unordered
LinkedHashMap - ordered by insertion order
SplayTreeMap - ordered by sorted keys
By default, creating an instance using Map constructor (Map, Map.from, or Map.of) creates a
LinkedHashMap.
A map is created with a map literal or with a map constructor. To work with maps, we
import the dart:collection library.
map_literal.dart
void main() {
var data = {'name': 'John Doe', 'occupation': 'gardener'};
print(data);
print(data.runtimeType);
https://zetcode.com/dart/map/ 1/10
8/28/2021 Dart Map - working with a map collection in Dart language
print(words.runtimeType);
}
Here we have a simple map with two pairs. The 'name' and 'occupation' are keys, the 'John
Doe' and 'gardener' are values.
In the second map, we also specify the data types for the keys and values. The data types
precede the curly brackets.
$ dart map_literal.dart
{name: John Doe, occupation: gardener}
_InternalLinkedHashMap<String, String>
{1: sky, 2: falcon, 3: rock}
_InternalLinkedHashMap<int, String>
map_length.dart
void main() {
var fruit = {1: 'Apple', 2: 'Orange'};
print(fruit.length);
The example creates a simple map collection and prints its size.
$ dart map_length.dart
2
There are 2 elements in the map
check_if_empty.dart
https://zetcode.com/dart/map/ 2/10
8/28/2021 Dart Map - working with a map collection in Dart language
void main() {
var words = {
1: 'sky',
2: 'fly',
3: 'ribbon',
4: 'falcon',
5: 'rock',
6: 'ocean',
7: 'cloud'
};
print(words.isEmpty);
print(words.isNotEmpty);
print('---------------');
words.clear();
print(words.isEmpty);
print(words.isNotEmpty);
}
$ dart check_if_empty.dart
false
true
---------------
true
false
adding.dart
void main() {
var fruit = {1: 'Apple', 2: 'Orange'};
fruit[3] = 'Banana';
print(fruit);
https://zetcode.com/dart/map/ 3/10
8/28/2021 Dart Map - working with a map collection in Dart language
print(val2);
}
fruit[3] = 'Banana';
Since there is already a pair with key 3, the new pair is not added to the map. The method
returns Banana.
The 4: 'Cherry' pair is added to the map. The method returns Cherry.
$ dart adding.dart
{1: Apple, 2: Orange, 3: Banana}
{1: Apple, 2: Orange, 3: Banana}
Banana
{1: Apple, 2: Orange, 3: Banana, 4: Cherry}
Cherry
removing.dart
void main() {
var words = {
1: 'sky',
2: 'fly',
3: 'ribbon',
4: 'falcon',
5: 'rock',
6: 'ocean',
7: 'cloud'
};
words.remove(1);
print(words);
https://zetcode.com/dart/map/ 4/10
8/28/2021 Dart Map - working with a map collection in Dart language
words.clear();
print(words);
}
words.remove(1);
The removeWhere deletes all pairs whose values (words) begin with letter f.
words.clear();
$ dart remove_elements.dart
{2: fly, 3: ribbon, 4: falcon, 5: rock, 6: ocean, 7: cloud}
{3: ribbon, 5: rock, 6: ocean, 7: cloud}
{}
from_iterables.dart
void main() {
var letters = ['I', 'II', 'V', 'X', 'L'];
var numbers = [1, 2, 5, 10, 50];
$ dart from_iterables.dart
{I: 1, II: 2, V: 5, X: 10, L: 50}
https://zetcode.com/dart/map/ 5/10
8/28/2021 Dart Map - working with a map collection in Dart language
With the collection if and for forms, we can dynamically create maps in Dart. The syntax is
close to list comprehensions known from Haskell or Python.
collection_if_for.dart
void main() {
var words = ['sky', 'cloud', 'sod', 'worm', 'put', 'water', 'cup'];
var i = 0;
var data = {
for (var e in words)
if (e.length == 3) i++: e
};
print(data);
}
The example creates a map with a collection if/for form. We create a map from a list of
words, where we include only words that have three characters. To each word, we assign a
numeric key.
$ dart collection_if_for.dart
{0: sky, 1: sod, 2: put, 3: cup}
merging.dart
void main() {
var f1 = {1: 'Apple', 2: 'Orange'};
var f2 = {3: 'Banana'};
var f3 = {4: 'Mango'};
https://zetcode.com/dart/map/ 6/10
8/28/2021 Dart Map - working with a map collection in Dart language
print(fruit3);
}
In the example, we have three maps. We merge them with addAll and the spread operator.
$ dart merging.dart
{1: Apple, 2: Orange, 3: Banana, 4: Mango}
{1: Apple, 2: Orange, 3: Banana, 4: Mango}
{1: Apple, 2: Orange, 3: Banana, 4: Mango}
Dart containsKey/containsValue
With containsKey and containsValue, we can determine if a map contains a specific key and
value.
contains.dart
void main() {
var myMap = {1: 'Apple', 2: 'Orange', 3: 'Banana'};
print(myMap.containsKey(1));
print(myMap.containsKey(3));
print(myMap.containsValue('Apple'));
print(myMap.containsValue('Cherry'));
}
The example uses the containsKey and containsValue methods for a small map.
$ dart contains.dart
true
true
true
false
iteration.dart
void main() {
var fruit = {1: 'Apple', 2: 'Banana', 3: 'Cherry', 4: 'Orange'};
fruit.forEach((key, val) {
print('{ key: $key, value: $val}');
});
print('---------------------------');
https://zetcode.com/dart/map/ 7/10
8/28/2021 Dart Map - working with a map collection in Dart language
fruit.entries.forEach((e) {
print('{ key: ${e.key}, value: ${e.value} }');
});
print('---------------------------');
fruit.forEach((key, val) {
print('{ key: $key, value: $val}');
});
With forEach method, we print the key/value pairs of the fruit map.
fruit.entries.forEach((e) {
print('{ key: ${e.key}, value: ${e.value} }');
});
In a similar fashion, we loop over the entry objects of the fruit map.
Finally, with for statements, we go through the keys and values separately.
$ dart iteration.dart
{ key: 1, value: Apple}
{ key: 2, value: Banana}
{ key: 3, value: Cherry}
{ key: 4, value: Orange}
---------------------------
{ key: 1, value: Apple }
{ key: 2, value: Banana }
{ key: 3, value: Cherry }
{ key: 4, value: Orange }
---------------------------
1
2
3
4
Apple
Banana
Cherry
Orange
https://zetcode.com/dart/map/ 8/10
8/28/2021 Dart Map - working with a map collection in Dart language
sorting.dart
import 'dart:collection';
void main() {
var fruit = new SplayTreeMap<int, String>();
fruit[0] = 'Banana';
fruit[5] = 'Plum';
fruit[6] = 'Strawberry';
fruit[2] = 'Orange';
fruit[3] = 'Mango';
fruit[4] = 'Blueberry';
fruit[1] = 'Apple';
print(fruit);
fruit.forEach((key, val) {
print('{ key: $key, value: $val}');
});
We have a map of fruit. By default, the pairs are ordered by the keys -- numerically, in
ascending order.
We create a sorted map by values from the original map. We pass the from function a
comparison function, which compares the values of the pairs.
$ dart sorting2.dart
{0: Banana, 1: Apple, 2: Orange, 3: Mango, 4: Blueberry, 5: Plum, 6: Strawberry}
{ key: 0, value: Banana}
{ key: 1, value: Apple}
{ key: 2, value: Orange}
{ key: 3, value: Mango}
{ key: 4, value: Blueberry}
{ key: 5, value: Plum}
https://zetcode.com/dart/map/ 9/10
8/28/2021 Dart Map - working with a map collection in Dart language
{ key: 6, value: Strawberry}
{1: Apple, 0: Banana, 4: Blueberry, 3: Mango, 2: Orange, 5: Plum, 6: Strawberry}
https://zetcode.com/dart/map/ 10/10