-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.dart
66 lines (49 loc) · 2.45 KB
/
map.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// En otros lenguajes se conoce como diccionario
main(List<String> args) {
Map <String, String> mi = { 'one':'first', 'two': 'second'};
print(mi['two']); /*second*/
mi['tree'] = 'third';
print(mi); //{one: first, two: second, tree: third}
print(mi.runtimeType); /*JsLinkedHashMap<String, String>*/
Map<String, String> m = new Map();
m.addAll({"one": "first number"});
print(m); /* {one: first number} */
m.addAll({"two": "second"});
print(m); /* {one: first number, two: second} */
/* -------------------------------------------------------------------------- */
/* Agregar un valor si no existe una llave */
/* -------------------------------------------------------------------------- */
Map<int, int> mne = {1:10, 2:20, 3:30};
mne.putIfAbsent(5, (){
return 50;
});
print(mne); /*{1: 10, 2: 20, 3: 30, 5: 50} si es q existe no hace nada*/
/* -------------------------------------------------------------------------- */
/* recorrer */
/* -------------------------------------------------------------------------- */
m.forEach((key, value) {
print("$key $value");
});
/* -------------------------------------------------------------------------- */
/* actualizar */
/* -------------------------------------------------------------------------- */
m.update("one", (value) => "first");
print(m); /* {one: first, two: second} */
/* -------------------------------------------------------------------------- */
/* eliminar */
/* -------------------------------------------------------------------------- */
m.remove("two");
print(m); /* {one: first} */
m.addAll({"tree": "thirth"});
print(m); /* {one: first, tree: thirth} */
m.removeWhere((key, value) => key == "tree");
print(m); /* {one: first} */
/* -------------------------------------------------------------------------- */
/* Obtener keys y values */
/* -------------------------------------------------------------------------- */
Map<String, String> mkv = {'a': 'A', 'b':'B'};
List<String> k = mkv.keys.toList();
print('claves $k');/*claves [a, b]*/
List<String> v = mkv.values.toList();
print('valores $v');/*valores [A, B]*/
}