????Map 在日常生活中最常見的集合方式最欠,而 Dart 中的 Map 相較于 Android 中的 Map 略有不同,使用會(huì)更加靈活惩猫;小菜今天盡可能系統(tǒng)的學(xué)習(xí)一下芝硬;
Map
????Map 是一個(gè) key-value 鍵值對(duì)的集合對(duì)象,其 key 和 value 是一對(duì)多的關(guān)系轧房;類似于 Android 中 Map拌阴,Dart Map 也分為 HashMap 無序的 Map 集合、LinkedHashMap 插入有序的 Map 集合以及 SplayTreeMap 已排序好的 Map 集合锯厢;主要在 dart.core 核心庫中皮官;
????Map 與 List 都屬于泛型類,其中 key-value 均不指定具體類型实辑,默認(rèn)為 dynamic捺氢;
構(gòu)造函數(shù)
????Map 提供了眾多的構(gòu)造函數(shù),小菜逐一嘗試一下剪撬;
1. Map()
????Map() 默認(rèn)構(gòu)造函數(shù)會(huì)實(shí)現(xiàn)一個(gè) LinkedHashMap 對(duì)象摄乒,相同的 key 后面的會(huì)覆蓋之前的 value;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
Map map01 = Map();
map01['name'] = 'ACE';
map01['age'] = 18;
map01['isChecked'] = false;
map01['isChecked'] = true;
print('Map -> $map -> $map01');
I/flutter ( 8933): Map -> {name: ACE, age: 18, isChecked: false} -> {name: ACE, age: 18, isChecked: true}
2. Map.of()
????Map.of() 創(chuàng)建一個(gè)與其他 Map 對(duì)應(yīng) key-value 的 LinkedHashMap 對(duì)象残黑;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
Map map02 = Map.of(map);
map02['isChecked'] = true;
print('Map -> $map -> $map02');
I/flutter ( 8933): Map -> {name: ACE, age: 18, isChecked: false} -> {name: ACE, age: 18, isChecked: true}
3. Map.from()
????Map.from() 創(chuàng)建一個(gè)與其他 Map 對(duì)應(yīng) key-value 的 LinkedHashMap 對(duì)象馍佑;同時(shí) key-value 數(shù)據(jù)類型可以是其他 Map 對(duì)應(yīng)的數(shù)據(jù)類型及子類;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
Map map03 = Map.from(map);
map03['isChecked'] = true;
print('Map -> $map -> $map03');
I/flutter ( 8933): Map -> {name: ACE, age: 18, isChecked: false} -> {name: ACE, age: 18, isChecked: true}
4. Map.unmodifiable()
????Map.unmodifiable() 創(chuàng)建一個(gè)不可修改的與其他 Map 對(duì)應(yīng) key-value 的 LinkedHashMap 對(duì)象梨水;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
Map map04 = Map.unmodifiable(map);
// 異常 Unsupported operation: Cannot modify unmodifiable map
// map04['isChecked'] = true;
print('Map -> $map -> $map04');
I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false} -> {name: ACE, age: 18, isChecked: false}
5. Map.fromIterable()
????Map.fromIterable() 創(chuàng)建一個(gè)從 Iterable 中獲取對(duì)應(yīng) key-value 的 LinkedHashMap 對(duì)象拭荤;
List list = ['ACE', 18, false];
Map map05 = Map.fromIterable(list);
Map map06 = Map.fromIterable(list, key: (item) => item.toString(), value: (item) => item.toString());
print('Map -> $map05 -> $map06');
I/flutter ( 8933): Map -> {ACE: ACE, 18: 18, false: false} -> {ACE: ACE, 18: 18, false: false}
6. Map.fromIterables()
????Map.fromIterables() 創(chuàng)建一個(gè)給定 key 和 value 的 LinkedHashMap 對(duì)象;
List keyList = ['name', 'age', 'isChecked'];
List valueList = ['ACE', 18, false];
Map map07 = Map.fromIterables(keyList, valueList);
print('Map -> $map07');
I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false}
7. Map.fromEntries()
????Map.fromEntries() 創(chuàng)建一個(gè)新的 Map疫诽,可以配合 List 等進(jìn)行排序舅世;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
Map map08 = Map.fromEntries(map.entries.toList());
print('Map -> $map -> $map08');
List list = ['aaa', 'ddd', 'mmm', 'bbb'];
Map map05 = Map.fromIterable(list);
Map map09 = Map.fromEntries(map05.entries.toList()..sort((e1, e2) => e1.value.compareTo(e2.value)));
print('Map -> $map05 -> $map09');
I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false} -> {name: ACE, age: 18, isChecked: false}
I/flutter (21830): Map -> {aaa: aaa, ddd: ddd, mmm: mmm, bbb: bbb} -> {aaa: aaa, bbb: bbb, ddd: ddd, mmm: mmm}
8. Map.castFrom()
????Map.castFrom() 創(chuàng)建一個(gè)類型與其他 Map key-value 類型一致的 Map旦委;
Map<String, dynamic> map = {'name': 'ACE', 'age': 18, 'isChecked': false};
Map map10 = Map.castFrom(map);
Map map11 = Map.castFrom<String, dynamic, String, dynamic>(map);
print('Map -> $map -> $map10 -> $map11');
// 異常,類型不匹配
Map map12 = Map.castFrom<String, dynamic, String, String>(map);
print('Map -> $map -> $map10 -> $map12');
I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false} -> {name: ACE, age: 18, isChecked: false} -> {name: ACE, age: 18, isChecked: false}
非構(gòu)造函數(shù)方法
1. containsKey & containsValue
????containsKey & containsValue 判斷 Map 中是否有對(duì)應(yīng)相同的 key & value雏亚,返回 bool 類型缨硝;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
print('Map -> ${map.containsKey('name')} -> ${map.containsValue('ACE')} -> ${map.containsKey('address')} -> ${map.containsKey('Hello!')}');
I/flutter (21830): Map -> true -> true -> false -> false
2. addEntries & addAll
????addEntries & addAll 用于合并兩個(gè)同類型的 Map,若有相同 key 后面的 Map 會(huì)替換之前 Map 對(duì)應(yīng)的 key-value罢低;不同之處在于 addAll 是添加整個(gè) Map 而 addEntries 是通過 Iterable 添加查辩;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
Map map01 = {'name': '和尚', 'address': '北京'};
print('Map -> $map -> $map01');
map.addEntries(map01.entries);
// map.addAll(map01);
print('Map -> $map -> $map01');
I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false} -> {name: 和尚, address: 北京}
I/flutter (21830): Map -> {name: 和尚, age: 18, isChecked: false, address: 北京} -> {name: 和尚, address: 北京}
3. forEach
????forEach 用于遍歷 Map 集合中各 key-value 在遍歷過程中不允許增加和刪除鍵值對(duì);
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
print('Map -> $map');
map.forEach((key, value) {
if (key == 'name') value = '和尚';
print('Map -> $key -> $value');
});
I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false}
I/flutter (21830): Map -> name -> 和尚
I/flutter (21830): Map -> age -> 18
I/flutter (21830): Map -> isChecked -> false
4. putIfAbsent
????putIfAbsent 若 Map 中含有對(duì)應(yīng)的 key-value 則返回 key网持,否則將新的 key-value 添加到 Map 中宜岛;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
print('Map -> $map');
map.putIfAbsent('name', () => '和尚');
print('Map -> $map -> ${map.putIfAbsent('name', () => '和尚')}');
map.putIfAbsent('address', () => '北京');
print('Map -> $map');
I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false}
I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false} -> ACE
I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false, address: 北京}
5. update & updateAll
????update & updateAll 用于更新 Map 中 key-value;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
print('Map -> $map');
map.update('name', (value) => '和尚');
print('Map -> $map');
map.update('address', (value) => 'Hello!', ifAbsent: () => '北京');
print('Map -> $map');
map.updateAll((key, value) {
if (key == 'name') return '和尚';
if (key == 'age') return value * 2;
if (key == 'isChecked') return !value;
});
print('Map -> $map');
I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false}
I/flutter (21830): Map -> {name: 和尚, age: 18, isChecked: false}
I/flutter (21830): Map -> {name: 和尚, age: 18, isChecked: false, address: 北京}
I/flutter (21830): Map -> {name: 和尚, age: 36, isChecked: true, address: null}
6. remove & removeWhere & clear
????remove 用于刪除 Map 中單個(gè) key-value翎碑,若不存在對(duì)應(yīng)的 key-value 則無任何操作谬返;removeWhere 用于通過條件進(jìn)行批量刪除,小菜嘗試把 Map value 不是 String 類型的刪除日杈;clear 是通用的清空 Map 集合遣铝;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
print('Map -> $map');
map.remove('age');
print('Map -> $map');
map.remove('address');
print('Map -> $map');
map.removeWhere((key, value) {
return value.runtimeType != String;
});
print('Map -> $map');
map.clear();
print('Map -> $map');
I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false}
I/flutter (21830): Map -> {name: ACE, isChecked: false}
I/flutter (21830): Map -> {name: ACE, isChecked: false}
I/flutter (21830): Map -> {name: ACE}
I/flutter (21830): Map -> {}
7. cast
????cast 與 Map.castFrom() 類似,用于將 Map 數(shù)據(jù)類型提升至其他 Map 一致莉擒;
Map<String, dynamic> map = {'name': 'ACE', 'age': 18, 'isChecked': false};
Map map13 = map.cast();
print('Map -> $map -> $map13 -> ${map.runtimeType} -> ${map13.runtimeType}');
Map<dynamic, dynamic> map14 = map.cast();
print('Map -> $map -> $map14 -> ${map.runtimeType} -> ${map14.runtimeType}');
I/flutter (28054): Map -> {name: ACE, age: 18, isChecked: false} -> {name: ACE, age: 18, isChecked: false} -> _InternalLinkedHashMap<String, dynamic> -> CastMap<String, dynamic, dynamic, dynamic>
I/flutter (28054): Map -> {name: ACE, age: 18, isChecked: false} -> {name: ACE, age: 18, isChecked: false} -> _InternalLinkedHashMap<String, dynamic> -> CastMap<String, dynamic, dynamic, dynamic>
基本屬性
1. keys & values & entries
????Map 通過 get() 獲取集合中的 key-value 數(shù)據(jù)內(nèi)容酿炸,entries 為獲取對(duì)應(yīng)迭代的鍵值對(duì)集合;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
print('Map -> ${map.keys} -> ${map.keys.length} -> ${map.keys.toList().first} -> ${map.keys.toList().last}');
print('Map -> ${map.values} -> ${map.values.length} -> ${map.values.toList().first} -> ${map.values.toList().last}');
print('Map -> ${map.entries} -> ${map.entries.length} -> ${map.entries.toList().first} -> ${map.entries.toList().last}');
I/flutter (28054): Map -> (name, age, isChecked) -> 3 -> name -> isChecked
I/flutter (28054): Map -> (ACE, 18, false) -> 3 -> ACE -> false
I/flutter (28054): Map -> (MapEntry(name: ACE), MapEntry(age: 18), MapEntry(isChecked: false)) -> 3 -> MapEntry(name: ACE) -> MapEntry(isChecked: false)
2. isEmpty & isNotEmpty
????isEmpty & isNotEmpty 用于判斷 Map 是否為空集合涨冀;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
print('Map -> ${map.isEmpty} -> ${map.isNotEmpty}');
I/flutter (28054): Map -> false -> true
3. length
????length 用于獲取 Map 中鍵值對(duì)數(shù)量填硕;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
print('Map -> ${map.length}');
I/flutter (28054): Map -> 3
小擴(kuò)展
Map.of() & Map.from()
????Map.of() 和 Map.from() 都是創(chuàng)建與其他 Map key-value 一致的 LinkedHashMap 對(duì)象;區(qū)別在于 Map.of() 必須與其他的 Map 數(shù)據(jù)類型一致鹿鳖;
Map<dynamic, dynamic> map = {'name': 'ACE', 'age': 18, 'isChecked': false};
Map<String, dynamic> map03 = Map.from(map);
map03['isChecked'] = true;
print('Map -> $map -> $map03');
// 異常 type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'
Map<String, dynamic> map02 = Map.of(map);
map02['isChecked'] = true;
print('Map -> $map -> $map02');
????小菜對(duì) Map 的嘗試暫時(shí)告一段落扁眯,并非很高深的內(nèi)容,只是有個(gè)別屬性小菜用的不熟悉翅帜,進(jìn)行系統(tǒng)的了解一下姻檀;如有錯(cuò)誤,請(qǐng)多多指導(dǎo)涝滴!
來源: 阿策小和尚