-
update()
對指定的key的value做出修改
Map<String,int> map23 = {"a":1,"b":2,"c":3};
//key存在 根據(jù)參數(shù)函數(shù)修改其值,key不存在 報錯
int result3 = map23.update("a", (value)=>(value*2));
//key不存在 但有ifAbsent參數(shù) 返回ifAbsent函數(shù)的值 并添加到map中
int result4 = map23.update("d", (value)=>(value*2),ifAbsent: ()=>(10));
-
updateAll()
根據(jù)參數(shù)函數(shù)的規(guī)則,批量修改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()
根據(jù)條件批量刪除
Map<String,int> map10 = {"a":1,"b":2,"c":3,"d":4,"e":5};
//刪除掉 符合參數(shù)函數(shù)的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()
遍歷每個元素 根據(jù)參數(shù)函數(shù)眼刃,對keyvalue做出修改镊折,可轉(zhuǎn)換成其他泛型的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();