Dart(五)Map的属性和方法实例

声明

不指定泛型

    var map1 = {'aa':'aaa','bb':22,'cc':true};//直接赋初始值
    //print(map1);//{aa: aaa, bb: 22, cc: true}
    Map map3 = {"a":"a1","b":"b1"};//直接赋初始值
    //print(map3);//{a: a1, b: b1}
    map3["c"] ="c";
    //print(map3);//{a: a1, b: b1, c: c}

    var map2 = new Map();
    map2['dd']=3.3;
    map2[2] = 'ddd22';
//    print(map2);//{dd: 3.3, 2: ddd22}
//    print(map2[3]);//null   如果不存在,则是null
    map2[3]= null;//key存在  value存在 value的值为null
//    print(map2.length);//3    长度

指定泛型

    var map333 = <String,String>{};
    Map<int,String> map4 = new Map();
    map4[1]="a";
    map4[2] = "b";
//    print(map4);//{1: a, 2: b}
    Map<String,int> map5 = {"a":1,"b":2};
//    print(map5);//{a: 1, b: 2}
属性
    Map<String,int> map6 = {"a":1,"b":2};
//    print(map6.length);//2  长度
//    print(map6.isNotEmpty);//true  是否不为空
//    print(map6.isEmpty);//false   是否为空
//    print(map6.keys);//(a, b)   key的集合
//    print(map6.values);//(1, 2)  value的集合
//    print(map6.entries);//(MapEntry(a: 1), MapEntry(b: 2))   map迭代的键值对集合
方法

新增一个key-value

    Map<String,int> map7 = {"a":1,"b":2,"c":3,"d":4,"e":5};
    //新增一个key value
    map7["f"] = 6;//新增一个不存在的key
//    print(map7);//{a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}

修改一个key的value

    Map<String,int> map8 = {"a":1,"b":2,"c":3,"d":4,"e":5};
    map8["a"] = 11;
    //print(map8);//{a: 11, b: 2, c: 3, d: 4, e: 5}

update() 对指定的key的value做出修改

    Map<String,int> map23 = {"a":1,"b":2,"c":3};
    int result3 = map23.update("a", (value)=>(value*2));//key存在  根据参数函数修改其值
//    print(result3);//2
//    print(map23);//{a: 2, b: 2, c: 3}
//    int result4 = map23.update("d", (value)=>(value*2));//key不存在  报错
    int result4 = map23.update("d", (value)=>(value*2),ifAbsent: ()=>(10));//key不存在  但有ifAbsent参数 返回ifAbsent函数的值  并添加到map中
//    print(result4);//10
//    print(map23);//{a: 2, b: 2, c: 3, d: 10}

updateAll() 根据参数函数的规则,批量修改map

    Map<String,int> map24 = {"a":1,"b":2,"c":3};
    map24.updateAll((String key,int value){
      return value*2;
    });//
//    print(map24);//{a: 2, b: 4, c: 6}
Map<String,int> map25 = {"a":1,"b":2,"c":3};
    map25.updateAll((String key,int value){
      if(key=="a"){return 10;}
      if(key=="b"){return 20;}
      return value*2;
    });//
//    print(map25);//{a: 10, b: 20, c: 6}

remove() 删除一个key

    Map<String,int> map9 = {"a":1,"b":2,"c":3,"d":4,"e":5};
    map9.remove("b");
    //print(map9);//{a: 11, c: 3, d: 4, e: 5}

removeWhere() 根据条件批量删除

    Map<String,int> map10 = {"a":1,"b":2,"c":3,"d":4,"e":5};
    map10.removeWhere((key,value)=>(value>3));//删除掉 符合参数函数的keyvalue对
    //print(map10);//{a: 1, b: 2, c: 3}

containsKey() 是否包含key

    Map<String,int> map11 = {"a":1,"b":2,"c":3,"d":4,"e":5};
//    print(map11.containsKey("a"));//true   是否包含key
//    print(map11.containsKey("aa"));//false  是否包含key

containsValue() 是否包含value值

    Map<String,int> map17 = {"a":1,"b":2,"c":3};
//    print(map17.containsValue(1));//true
//    print(map17.containsValue(4));//false

forEach() 遍历

    Map<String,int> map12 = {"a":1,"b":2,"c":3,"d":4,"e":5};
    map12.forEach((String key,int value){
//      print("$key  $value");
//       a  1
//       b  2
//       c  3
//       d  4
//       e  5
    });
//遍历时修改value值
    Map<String,int> map13 = {"a":1,"b":2,"c":3};
    map13.forEach((String key,int value){
//      print("$key  $value");
      map13["c"] = 4;
//       a  1
//       b  2
//       c  4
    });
//遍历时,新增或删除key  都会报错
    Map<String,int> map14 = {"a":1,"b":2,"c":3};
    map14.forEach((String key,int value){
//      print("$key  $value");
//      map14["d"] = 4;//  报错
//    map14.remove("a");//  报错
    });

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);
    });
//    print(map20);//{1: a, 2: b, 3: c}

clear() 清空map

    Map<String,int> map15 = {"a":1,"b":2,"c":3};
    map15.clear();
//    print(map15);//{}

addAll() 整体合并另一个map 泛型要一致

    Map<String,int> map16 = {"a":1,"b":2,"c":3};
    Map<String,int> other = {"a":1,"c":4,"d":7};
    map16.addAll(other);//key相同时value值后者覆盖前者,前者不存在时则添加进来
//    print(map16);//{a: 1, b: 2, c: 4, d: 7}

addEntries() 合并两个map 如果key有重复,被合并的map的value覆盖前者

    Map<String,int> map26 = {"a":1,"b":2,"c":3};
    Map<String,int> map27 = {"a":1,"b":4,"d":3,"e":5};
    map26.addEntries(map27.entries);
//    print(map26);//{a: 1, b: 4, c: 3, d: 3, e: 5}

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();
    map22["d"]=33;
//    print(map22);//{a: 1, b: 2, c: 3, d: 33}
欢迎添加我的微信号:zrf540592766
欢迎关注我的公众号:Flutter和Dart开发实践
让我们共同学习进步,It is never too late to learn!
image.png

推荐阅读更多精彩内容