-
update()
对指定的key的value做出修改
Map<String,int> map23 = {"a":1,"b":2,"c":3};
//key存在 根据参数函数修改其值,key不存在 报错
int result3 = map23.update("a", (value)=>(value*2));
//key不存在 但有ifAbsent参数 返回ifAbsent函数的值 并添加到map中
int result4 = map23.update("d", (value)=>(value*2),ifAbsent: ()=>(10));
-
updateAll()
根据参数函数的规则,批量修改map
Map<String,int> map24 = {"a":1,"b":2,"c":3};
map24.updateAll((String key,int value){
return value*2;
});
-
remove()
删除一个key
Map<String,int> map9 = {"a":1,"b":2,"c":3,"d":4,"e":5};
map9.remove("b");
-
removeWhere()
根据条件批量删除
Map<String,int> map10 = {"a":1,"b":2,"c":3,"d":4,"e":5};
//删除掉 符合参数函数的keyvalue对
map10.removeWhere((key,value)=>(value>3));
containsKey()
是否包含key
containsValue()
是否包含value值
-
forEach()
遍历时可以修改value值,遍历时,新增或删除key 都会报错
Map<String,int> map12 = {"a":1,"b":2,"c":3,"d":4,"e":5};
map12.forEach((String key,int value){
// print("$key $value");
});
-
map()
遍历每个元素 根据参数函数,对keyvalue做出修改,可转换成其他泛型的Map
Map<String,int> map19 = {"a":1,"b":2,"c":3};
Map<int,String> map20 = map19.map((String key,int value){
return new MapEntry(value, key);
});
clear()
清空map
addAll()
整体合并另一个map 泛型要一致,key相同时value值后者覆盖前者,前者不存在时则添加进来
addEntries()
合并两个map 如果key有重复,被合并的map的value覆盖前者
-
putIfAbsent()
存在key就获取值,不存在则添加到map 然后返回值
Map<String,int> map18 = {"a":1,"b":2,"c":3};
int result = map18.putIfAbsent("a", ()=>(2));//存在
print(result);//1 获取key的值
print(map18);//{a: 1, b: 2, c: 3} map不变
int result2 = map18.putIfAbsent("d", ()=>(2));//不存在
print(result2);//2 获取新的key的value
print(map18);//{a: 1, b: 2, c: 3, d: 2} map改变
-
cast()
泛型类型提升为其父祖类
Map<String,int> map21 = {"a":1,"b":2,"c":3};
Map<Object,Object> map22 = map21.cast();