• Dart 数据类型 Map


    Map 是一个无序的键值对(key-value)映射,通常被称为哈希或字典。

    声明方式
    (1)、字面量
           var map = { key1: value1, key2: value2 };
    (2)、构造函数
           var map = new Map();
           map['key'] = value;
      // 声明 - 字面量
      var person = {'name': 'bob', 'age': 20};
      print(person); // {name: bob, age: 20}
    
      // 声明 - 构造函数
      var p = new Map();
      p['name'] = 'kobe';
      p['age'] = 42;
      print(p); // {name: kobe, age: 42}
    属性
    (1)、map[key]:访问属性
    (2)、length → int :返回键值对的数量
    (3)、keys → Iterable<K> :返回一个只包含键名的可迭代对象
    (4)、values → Iterable<V> :返回一个只包含键值的可迭代对象
    (5)、isEmpty → bool :检测map是否为空
      // 访问属性
      print(person['name']); // bob
      // 返回键值对的数量
      print(person.length); // 2
      // 返回一个只包含键名的可迭代对象
      print(person.keys); // (name, age)
      // 返回一个只包含键值的可迭代对象
      print(person.values); // (bob, 20)
      // 检测map是否为空
      bool isEmpty = person.isEmpty;
      print(isEmpty); // false
    方法
    (1)、addAll(Map<K, V> other) → void :添加一个map
    (2)、containsKey(Object? key) → bool :判断map中的key是否存在
    (3)、putIfAbsent(K key, V ifAbsent()) → V :如果key存在,就不赋值;如果key不存在,就赋值
    (4)、remove(Object? key) → V? :删除指定的键值对
    (5)、removeWhere(bool test(K key, V value)) → void :根据条件进行删除
    (6)、clear() → void :清空map
      // addAll()
      person.addAll({'city': 'beijing', 'country': 'china'});
      print(person); // {name: bob, age: 20, city: beijing, country: china}
    
      // containsKey()
      bool hasCity = person.containsKey('city');
      print(hasCity); // true
    
      // putIfAbsent()
      person.putIfAbsent('city', () => 'hangzhou');
      print(person); // {name: bob, age: 20, city: beijing, country: china}
      person.putIfAbsent('gender', () => 'male');
      print(
          person); // {name: bob, age: 20, city: beijing, country: china, gender: male}
    
      // remove()
      final removedValue = person.remove('country');
      print(removedValue); // china
      print(person); // {name: bob, age: 20, city: beijing, gender: male}
    
      // removeWhere()
      person.removeWhere((key, value) => key == 'gender');
      print(person); // {name: bob, age: 20, city: beijing}
    
      // clear()
      person.clear();
      print(person); // {}
    遍历
    (1)、forEach(void action(K key, V value)) → void
    (2)、map<K2, V2>(MapEntry<K2, V2> convert(K key, V value)) → Map<K2, V2>
      // forEach()
      final score = {'chinese': 145, 'math': 138, 'english': 142};
      score.forEach((key, value) {
        print('$key, $value');
      });
    
      // map() 给每一科成绩都加5分
      final newScore = score.map((key, value) => new MapEntry(key, value + 5));
      print(newScore); // {chinese: 150, math: 143, english: 147}
     
  • 相关阅读:
    Python使用inspect查看代码参数
    Python的hasattr() getattr() setattr()
    Module-GitBook使用手册
    Module-Hadoop技术文档
    Module-Git使用手册
    Module-Faker使用手册
    Module-Docker使用手册
    Module-DB2技术文档
    Module-Dask并行任务调度
    Module-Apache使用说明
  • 原文地址:https://www.cnblogs.com/rogerwu/p/16168907.html
Copyright © 2020-2023  润新知