Flutter - Dart -Map的基本使用

2024-05-20  本文已影响0人  rainy66

Map定义
不指定泛型

  var map1 = {'aa': 'aaa', 'bb': 'bbb', 'cc': true}; //直接赋值初始值
  print(map1); //{aa: aaa, bb: bbb, cc: true}

  Map map2 = {'a': 'a1', 'b': 'b1'}; //直接赋值初始值
  print(map2); //{a: a1, b: b1}

  map2['c'] = 'c1';
  print(map2); //{a: a1, b: b1, c: c1}

  var map3 = new Map();
  map3['a'] = 'a3';
  map3[2] = 'b3';
  print(map3); //{a: a3, 2: b3}
  print(map3[3]); //null 如果不存在,则为null
  map3[3] = null; //key存在 value存在 value的值为null
  print(map3); //{a: a3, 2: b3, 3: null}
 

指定泛型

var map4 = <String, String>{};
  Map<int, String> map5 = new Map(); //new也可以省略
  map5[1] = 'a5';
  map5[2] = 'b5';
  print(map5); //{1: a5, 2: b5}
  Map<String, int> map6 = {'a6': 1, 'b6': 2};
  print(map6); //{a6: 1, b6: 2}

Map常见属性

 Map<String, int> map7 = Map();
  map7['a7'] = 1;
  map7['b7'] = 2;
  map7['c7'] = 3;
  print(map7.length); //3 长度
  print(map7.isNotEmpty); //true  是否不为空
  print(map7.isEmpty); //flase  是否为空
  print(
      '${map7.keys},${map7.keys.runtimeType}'); // (a7, b7, c7),_CompactIterable<String>    Map的所有key
  print(
      '${map7.values},${map7.values.runtimeType}'); //(1, 2, 3),_CompactIterable<int>    Map的所有value
  print(
      '${map7.entries},${map7.entries.runtimeType}'); // (MapEntry(a7: 1), MapEntry(b7: 2), MapEntry(c7: 3)),MappedIterable<String, MapEntry<String, int> Map的所有键值对

  Map<String, int> map8 = Map();
  map8['a8'] = 1;
  print(map8); //{a8: 1}
  
  Map<String, int> map9 = {'a9': 1, 'b9': 2};
  map9['a9'] = 9;
  print(map9); //{a9: 9, b9: 2}  
  
  Map<String, int> map10 = {'a10': 1, 'b10': 2, 'c10': 3};
  var resultMap10 = map10.update('b10', (value) => value * 2);
  print(resultMap10); //4
  print(map10); //{a10: 1, b10: 4, c10: 3}
  
  var resultMap101 = map10.update('c', (value) => (value * 2),
      ifAbsent: () => (10)); //如果key不存在,但是有ifAbsent参数,返回idAbsent函数的值,并添加到map中
  print('$resultMap101,${resultMap101.runtimeType}'); //10,int
  print(map10); //{a10: 1, b10: 4, c10: 3, c: 10}
 
  Map<String, int> map11 = {'a11': 2, 'b11': 3, 'c11': 4};
  var resultMap11 = map11.updateAll((key, value) {
    return value * 2;
  });
  print(map11); //{a11: 4, b11: 6, c11: 8}
  map11.updateAll((key, value) {
    if (key == 'a11') {
      return value * 2;
    }
    if (key == 'c11') {
      return value + 1;
    }
    return 7; //如果没有这行,b11对应的value为null 这里可以看出updateall会影响所有的键值对
  });
  print(map11); //{a11: 8, b11: 7, c11: 9}
  
  Map<String, int> map12 = {'a12': 2, "b12": 1};
  map12.remove('a12');
  print(map12); //{b12: 1}
  map12.remove('c12'); //删除一个不存在的key,毫无影响,无报错无警告
  print(map12); //{b12: 1}  
  
 Map<String, int> map13 = {'a13': 3, 'b13': 4, 'c13': 1};
 map13.removeWhere((key, value) => value > 3);
 print(map13); //{a13: 3, c13: 1}  
 
   Map<String ,int> map14 = {'a14':1};
   bool resultMap14 = map14.containsKey('a11'); //false
   bool resultMap141 = map14.containsValue(1); //true

  Map<String, int> map15 = {'a15': 1, 'b15': 2, 'c': 3, 'd': 4, 'e': 5};
  map15.forEach((key, value) {
    print('$key,$value');
    /*
    a15,1
    b15,2
     c,3
     d,4
     e,5
    */
  });
   map15.forEach((key, value) {
    //  if(key == 'a15'){value = 8;}
    map15['a15'] = 8;
   });
   print(map15); //{a15: 8, b15: 2, c: 3, d: 4, e: 5}
   
   Map<String,int> map16 = {'a16':7,"b16":5,'c16':4};
   Map<int,String> map17 = map16.map((key, value) {
       return MapEntry(value, key);
   });
   print(map17);
   
  Map<String,int> map18 = {'a18':1,'b18':7,'a19':2};
  Map<String,int> map19 = {'a19':9};
  map18.addAll(map19);
  print(map18); //{a18: 1, b18: 7, a19: 9}   
  
  Map<String,int> map20 = {'a20':2,'b20':3};
  Map<String,int> map21 = {'a21':5,'b21':9};
  map20.addEntries(map21.entries);
  print(map20); //{a20: 2, b20: 3, a21: 5, b21: 9}  
  
 Map<String,int> map22 = {'a22':3,'b22':4};
 var resultMap22 = map22.putIfAbsent('a22', () => 2); //存在key则返回value,查不到则返回 2 不修改Map
 print('$resultMap22,$map22');//3,{a22: 3, b22: 4} 
 
 var resultMap221 = map2.putIfAbsent('a2', () => 1);
 print('$resultMap221,$map22'); //1,{a22: 3, b22: 4} //存在key则返回value,查不到则返回 1 不修改Map 

 Map<String,int> map23 = {'a33':3};
 Map<Object,Object> map24 = map23.cast();
 print('$map23,${map23.runtimeType},$map24,${map24.runtimeType}'); //{a33: 3},_InternalLinkedHashMap<String, int>,{a33: 3},CastMap<String, int, Object, Object>


 Map<String,int> map25 = {'a25':2,'b25':3};
 map25.clear();
 print('$map25,${map25.runtimeType}'); //{},_InternalLinkedHashMap<String, int>
 
上一篇下一篇

猜你喜欢

热点阅读