This is the 25th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Maps are most commonly used in daily life. Dart maps are slightly different from Android maps and are more flexible to use. Xiao CAI today as systematic as possible to learn;
Map
A Map is a collection of key-value pairs. The key and value are one-to-many. Similar to Android maps, Dart maps are divided into HashMap unordered Map collection, LinkedHashMap inserted in order Map collection, and SplayTreeMap sorted Map collection. Dart. core core library;
Map and List are generic classes. Key-value does not specify a specific type. The default value is Dynamic.
The constructor
Map provides numerous constructors, so try them one by one;
1. Map()
The Map() default constructor implements a LinkedHashMap object that overwrites the value after the same key;
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}
Copy the code
2. Map.of()
Map.of() creates a LinkedHashMap object with a key-value corresponding to another Map.
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}
Copy the code
3. Map.from()
Map.from() creates a LinkedHashMap object with a key-value corresponding to another Map. The key-value data type can be the data type or subclass of other maps.
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}
Copy the code
4. Map.unmodifiable()
Map.unmodifiable() Creates an unmodifiable LinkedHashMap object with the corresponding key-value from other maps;
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}Copy the code
5. Map.fromIterable()
Map.fromiterable () creates a LinkedHashMap that retrieves the corresponding key-value fromIterable;
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}
Copy the code
6. Map.fromIterables()
Map.fromiterables () creates a LinkedHashMap object for a given key and value;
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}
Copy the code
7. Map.fromEntries()
Map.fromentries () creates a new Map that can be sorted with lists, etc.
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}Copy the code
8. Map.castFrom()
Map.castfrom () creates a Map whose type is the same as other Map key-value types.
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}Copy the code
Non-constructor methods
1. containsKey & containsValue
ContainsKey & containsValue Checks whether there are identical keys and values in the Map. The value is returned as 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
Copy the code
2. addEntries & addAll
AddEntries & addAll is used to combine two maps of the same type. If they have the same key, the subsequent Map replaces the key-value of the previous Map. The difference is that addAll adds the entire Map while addEntries are added through Iterable;
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false}; Map map01 = {'name': 'monk ', 'address':' Beijing '}; 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: bonze, address: Beijing} I/flutter (21830): Map -> {name: bonze, address: Beijing} I/flutter (21830): Map -> {name: bonze, address: Beijing} Map -> {name: monk, age: 18, isChecked: false, address: Beijing} -> {name: monk, address: Beijing}Copy the code
3. forEach
ForEach is used to traverse key-values in a Map set. During traversal, key-value pairs cannot be added or deleted.
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false}; print('Map -> $map'); Map. ForEach ((key, value) {if (key == 'name') value = 'monk '; print('Map -> $key -> $value'); }); I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false} I/flutter (21830): Map -> name -> Monk I/flutter (21830): Map -> age -> 18 I/flutter (21830): Map -> isChecked -> falseCopy the code
4. putIfAbsent
PutIfAbsent Returns the key if the Map contains the corresponding key-value. Otherwise, the new key-value is added to the Map.
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false}; print('Map -> $map'); Map.putifabsent ('name', () => 'monk '); Print (' Map - > $Map - > ${Map. PutIfAbsent (' name ', () = > 'monks')} "); Map.putifabsent ('address', () => 'Beijing '); print('Map -> $map'); I/flutter (21830): Map -> {name: ACE, age: 18, isChecked: false} I/flutter (21830): Map -> {name: ACE, age: 19, isChecked: false} -> I/flutter (21830): Map -> {name: flutter, age: 18, isChecked: false, address: flutter}Copy the code
5. update & updateAll
Update & updateAll Used to update the key-value in the Map.
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false}; print('Map -> $map'); Map.update ('name', (value) => 'monk '); print('Map -> $map'); map.update('address', (value) => 'Hello! ', ifAbsent: () => "); print('Map -> $map'); Map. updateAll((key, value) {if (key == 'name') return 'monk '; 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: a, age: 18, isChecked: false} 18, isChecked: false} I/flutter (21830): Map -> {name: the master, age: 18, isChecked: false, address: Beijing} I/flutter (21830): Map -> {name: monk, age: 36, isChecked: true, address: null}Copy the code
6. remove & removeWhere & clear
Remove Is used to delete a single key-value from a Map. If no corresponding key-value exists, no operation is performed. RemoveWhere is used to batch delete maps whose values are not strings. Clear is a generic collection of clear maps;
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 -> {}Copy the code
7. cast
Cast is similar to map.castfrom (), which is used to promote the Map data type to the same as other maps.
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>
Copy the code
Basic attributes
1. keys & values & entries
Map obtains key-value data content in the collection through get(), and entries are the collection of key-value pairs for corresponding iterations.
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)
Copy the code
2. isEmpty & isNotEmpty
IsEmpty & isNotEmpty is used to determine whether a Map is an empty set.
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
print('Map -> ${map.isEmpty} -> ${map.isNotEmpty}');
I/flutter (28054): Map -> false -> true
Copy the code
3. length
Length is used to obtain the number of key-value pairs in the Map.
Map map = {'name': 'ACE', 'age': 18, 'isChecked': false};
print('Map -> ${map.length}');
I/flutter (28054): Map -> 3
Copy the code
A small extension
Map.of() & Map.from()
Map.of() and map.from () create LinkedHashMap objects that match other Map key-values. The difference is that map.of () must be the same as other Map data types;
Map<dynamic, dynamic> map = {'name': 'ACE', 'age': 18, 'isChecked': false}; Map<String, dynamic> map03 = Map.from(map); map03['isChecked'] = true; print('Map -> $map -> $map03'); // exception 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');Copy the code
Xiao CAI’s attempt to Map has come to an end for the time being. It is not very advanced, but I am not familiar with some attributes of xiao CAI, so I will have a systematic understanding of it. If there are mistakes, please give more guidance!
Source: Little Monk A Ce