在Dart编程中,Map 是类似字典的数据类型,以键值形式(称为锁键)存在。地图数据类型中的数据类型没有限制。地图非常灵活,可以根据需求改变其大小。但是,需要注意的是,所有锁(键)在地图数据类型中都必须是唯一的。
我们可以通过两种方式声明 Map:
- 使用地图字面量
- 使用地图构造函数
映射字面量:
可以使用地图字面量声明地图,如下所示:
Syntax:
// Creating the Map using Map Literals
var map_name = { key1 : value1, key2 : value2, ..., key n : value n }
示例 1:
使用地图字面量创建地图
Dart
void main() {
// Creating Map using is literals
var gfg = {'position1' : 'Geek', 'position2' : 'for', 'position3' : 'Geeks'};
// Printing Its content
print(gfg);
// Printing Specific Content
// Key is defined
print(gfg['position1']);
// Key is not defined
print(gfg[0]);
}
Dart
void main() {
// Creating Map using is literals
var gfg = {'position1' : 'Geek' 'for' 'Geeks'};
// Printing Its content
print(gfg);
// Printing Specific Content
// Key is defined
print(gfg['position1']);
}
Dart
void main() {
// Creating Map
var gfg = {'position1' : 'Geeks' 'for' 'Geeks'};
// Printing Its content before insetion
print(gfg);
// Inserting a new vaplue in Map
gfg ['position0'] = 'Welcome to ';
// Printing Its content after insertion
print(gfg);
// Printing Specific Content
// Keys is defined
print(gfg['position0'] + gfg['position1']);
}
Dart
void main() {
// Creating Map using Contructors
var gfg = new Map();
// Inserting values into Map
gfg [0] = 'Geeks';
gfg [1] = 'for';
gfg [2] = 'Geeks';
// Printing Its content
print(gfg);
// Printing Specific Content
// Key is defined
print(gfg[0]);
}
Dart
void main() {
// Creating Map using Contructors
var gfg = new Map();
// Inserting values into Map
gfg [0] = 'Geeks';
gfg [0] = 'for';
gfg [0] = 'Geeks';
// Printing Its content
print(gfg);
// Printing Specific Content
// Key is defined
print(gfg[0]);
}
输出:
{position1: Geek, position2: for, position3: Geeks}
Geek
null
示例 2:
Dart
void main() {
// Creating Map using is literals
var gfg = {'position1' : 'Geek' 'for' 'Geeks'};
// Printing Its content
print(gfg);
// Printing Specific Content
// Key is defined
print(gfg['position1']);
}
输出:
{position1: GeekforGeeks}
GeekforGeeks
您已经注意到不同的字符串被连接为一个。
示例 3:
在 Map 中插入一个新值
Dart
void main() {
// Creating Map
var gfg = {'position1' : 'Geeks' 'for' 'Geeks'};
// Printing Its content before insetion
print(gfg);
// Inserting a new vaplue in Map
gfg ['position0'] = 'Welcome to ';
// Printing Its content after insertion
print(gfg);
// Printing Specific Content
// Keys is defined
print(gfg['position0'] + gfg['position1']);
}
输出:
{position1: GeeksforGeeks}
{position1: GeeksforGeeks, position0: Welcome to }
Welcome to GeeksforGeeks
地图构造函数:
Syntax:
// Creating the Map using Map Constructor
var map_name = new Map();
// Assigning value and key inside Map
map_name [ key ] = value;
示例 1:
使用地图构造函数创建地图
Dart
void main() {
// Creating Map using Contructors
var gfg = new Map();
// Inserting values into Map
gfg [0] = 'Geeks';
gfg [1] = 'for';
gfg [2] = 'Geeks';
// Printing Its content
print(gfg);
// Printing Specific Content
// Key is defined
print(gfg[0]);
}
输出:
{0: Geeks, 1: for, 2: Geeks}
Geeks
示例 2:
将相同的键分配给不同的元素
Dart
void main() {
// Creating Map using Contructors
var gfg = new Map();
// Inserting values into Map
gfg [0] = 'Geeks';
gfg [0] = 'for';
gfg [0] = 'Geeks';
// Printing Its content
print(gfg);
// Printing Specific Content
// Key is defined
print(gfg[0]);
}
输出:
{0: Geeks}
Geeks
您已经注意到其他两个值被简单地忽略了。