本篇文章总结了Dart中map的使用方式,由于map中的函数相对较少,没有做明确的顺序,代码如下:

void main(List<String> args) {print("这个demo用来演示Map中常用的属性和方法");//初始化Map//普通方式,不指定任何类型,这时testMap中K-V的类型均是dynamicMap testMap = Map();print("testMap: $testMap");testMap[1] = "哈哈哈";//强制使用指定类型初始化mapMap testMap1 = Map<int,String>();//不使用类型操作符,从另一个map中初始化新的map,此时新的map中含有另一个map中的资源Map testMap2 = Map.castFrom(testMap);//强制使用指定类型初始化map//下面的例子表示testMap1的类型为<num,String>,初始化Map时castFrom中map的类型为<int,String>//如果类型不匹配或者不兼容就会导致程序crashh//Map testMap3 = Map.castFrom<num,String,int,String>(testMap);//这行代码会出错,主要原因是testMap是<dynamic,dynamic>类型的,但是这里需要的是<int,String>类型的map//Map testMap4 = Map.castFrom<int,String,String,String>(testMap);//这行代码也会出错,因为无法将<String,String>类型的map转换为<int,String>类型的mapMap testMap3 = Map.castFrom<int,String,int,String>(testMap1);print("testMap3:$testMap3 and type is ${testMap3.runtimeType}");//无法使用指定参数类型,只能从另一个map中初始化一个新的mapMap testMap4 = Map.from(testMap);print("testMap4:$testMap4 and type is ${testMap4.runtimeType}");//无法使用指定类型参数,从一个实现了Iterable接口的集合中初始化mapvar testList = List<MapEntry<int,String>>();testList.add(MapEntry(1,"哈哈哈"));testList.add(MapEntry(2,"嘻嘻嘻"));Iterable testIterable = testList.take(testList.length);Map testMap5 = Map.fromEntries(testIterable);print("testMap5:$testMap5,and type is ${testMap5.runtimeType}");//不支持指定类型,创建一个默认的mapMap testMap6 = Map.identity();print("
testMap6:$testMap6,and type is ${testMap6.runtimeType}
");Map testMap7 = Map.fromIterable(testIterable,key: (item) => item.toString()+"哈哈哈",value: (item) => item.toString()+"嘻嘻嘻");print("
testMap7:$testMap7,and type is ${testMap7.runtimeType}
");//从一个可迭代的对象中获取key,从另一个可迭代的对象中获取value,然后使用两个Iterable创建出mapvar testList1 = List();var testList2 = List();testList1.add("one");testList1.add("two");testList1.add("three");testList1.add("one");testList2.add(1);testList2.add(2);testList2.add(3);testList2.add(4);Iterable testIterable1 = testList1.take(testList1.length);Iterable testIterable2 = testList2.take(testList2.length);Map testMap8 = Map.fromIterables(testIterable1, testIterable2);print("
testMap8:$testMap8, and type is ${testMap8.runtimeType}
");//从另一个Map中创建一个新的Map对象Map testMap9 = Map.of(testMap1);print("
通过Map.of创建Map对象:$testMap9,and type is ${testMap9.runtimeType}
");//创建一个不可修改的基于散列的mapMap testMap10 = Map.unmodifiable(testMap5);print("
通过Map.unmodifiable创建Map对象:$testMap10,and type is ${testMap10.runtimeType}
");//常用字段//需要注意的是:在调用这些字段的时候需要判断map是否为空,如果map为空则会抛出error,如:NoSuchMethodError: The getter 'length' was called on null.testMap8["哈哈哈"] = "呸呸呸";//获取map的长度:length字段print("
testMap8的长度:${testMap8.length}
");//获取所有的key,返回一个Iterableprint("
testMap8的key值${testMap8.keys}
");//获取所有的value,返回一个Iterableprint("
testMap8的value值:${testMap8.values}
");//isEmpty:查看是否为空,map中没有元素的时候返回true,注意不是map == nullprint("
testMap8是否为空:${testMap8.isEmpty}
");//isNotEmpty是否为非空,当map中至少有一个值的时候返回true,注意不是map == null,map != null 但是map中没有元素仍然返回falseprint("
testMap8是否为非空:${testMap8.isNotEmpty}
");//将map输出为Iterableprint("
testMap8的entries:${testMap8.entries}
");//常用方法testMap8[1.235] = "1.2222";testMap8[4] = "4";//当map中包含指定的key时返回true//比较的时候需要注意数据类型print("
$testMap8");bool containsKey1 = testMap8.containsKey("4");bool containsKey2 = testMap8.containsKey("one");bool containsKey3 = testMap8.containsKey(4);print("
testMap8的key中是否包含字符串4:$containsKey1");print("
testMap8的key中是否包含one:$containsKey2");print("
testMap8的key中是否包含数字4:$containsKey3");//当map中包含指定的value时返回true//比较的时候需要注意数据类型bool containsValue1 = testMap8.containsValue("4");bool containsValue2 = testMap8.containsValue(4);bool containsValue3 = testMap8.containsValue("哈哈");print("
testMap8的value中是否包含字符串4:$containsValue1");print("
testMap8的value中是否包含数字4:$containsValue2");print("
testMap8中是否包含字符串哈哈:$containsValue3");//通过Iterable<Map<K,V>>来添加数据List testList3 = List<MapEntry<String,int>>();testList3.add(MapEntry("咚咚咚", 22));testList3.add(MapEntry("哼哼哼", 100));Iterable testIterable3 = testList3.reversed;testMap8.addEntries(testIterable3);print("
使用addEntries添加数据之后的map:$testMap8");//通过addAll(Map<K,V>)来添加数据print("$testMap2");testMap8.addAll(testMap2);print("通过addAll添加数据之后的map:$testMap8");//这个方法可以看作强制判断Map中的数据是否符合<RK,RV>中指定的数据类型,如果存在不符合<RK,RV>指定的数据类型,就会出现errorMap<dynamic,dynamic> testCast = testMap8.cast();print("
testCast:$testCast");//取出当前map中的key和value在传入map中的函数中进行操作var testMapF = testMap8.map((key,value) => checkMap1(key, value));print("
使用map对之前的Map进行操作,并返回一个新的Map:$testMapF,and testMapF is ${testMapF.runtimeType}");print("
$testMap8");//删除符合条件的map中的元素testMap8.removeWhere((key,value) => key is int);//[4:4]这个元素被删除了print("删除testMap8中key是int类型的元素:$testMap8");//更新key中的信息,如果key不存在,会调用第三个参数中指定的函数,第三个参数为可选参数testMap8.update("one", (value) => "new"+ value.toString());print("
更新一个map中存在的key的元素:$testMap8");//从源码中可以看出:如果在map中找到了指定的key,就会根据第二个参数指定的规则去修改key对应的value的数据//如果没有找到指定的key,就会去查看第三个参数是否设置//如果第三个参数没有设置,直接抛出error//如果设置了第三个参数,就会指定第三个参数,执行完之后会在当前map中添加元素:未找到的key:第三个参数返回的数据//如果第三个参部的函数没有返回值(void),那么依然会添加元素:未找到的key:nulltestMap8.update(4, (value) => "new" + value.toString(),ifAbsent: checkMap2);print("更新一个map中不存在的元素并且指定ifAbsent参数:$testMap8");//自定义更新操作testMap8.updateAll((key,value) => checkMap3(key, value));print("自定义更新操作之后testMap8中的值:$testMap8");//循环输出Map中的值print("
testMap8中的值:");testMap8.forEach((key,value) => print("$key : $value"));//添加数据print("
添加数据之前:$testMap8");//添加一个已经存在的数据testMap8.putIfAbsent("three", () =>checkMap2());//添加一个不存在的数据testMap8.putIfAbsent("哒哒哒", () => checkMap2());print("添加数据之后$testMap8");//可以发现之前已经存在的key的数据并没有被改掉,之前没有的key会被添加进去并将第二个参数的函数的返回值作为value//清空map中的数据testMap8.clear();print("
testMap8中的数据被清空之后: $testMap8");
}//定义一个函数
MapEntry checkMap1(dynamic key,dynamic value){print("传入的key:$key,传入的value$value");MapEntry entry;entry = MapEntry("new" + key.toString(), "new" + value.toString());return entry;
}void checkMap2(){print("not found key");//return "not found key";
}dynamic checkMap3(dynamic key,dynamic value){if(key is num){//如果当前数据类型是num value就设置为key + 1return key + 1;}//如果数据类型不是num,直接将原来的value返回,这里一定要return,如果没有return,会默认return nullreturn value;
}

最终的控制台打印的数据如下:

这个demo用来演示Map中常用的属性和方法
testMap: {}
testMap3:{} and type is CastMap<int, String, int, String>
testMap4:{1: 哈哈哈} and type is _InternalLinkedHashMap<dynamic, dynamic>
testMap5:{1: 哈哈哈, 2: 嘻嘻嘻},and type is _InternalLinkedHashMap<dynamic, dynamic>testMap6:{},and type is _CompactLinkedIdentityHashMap<dynamic, dynamic>testMap7:{MapEntry(1: 哈哈哈)哈哈哈: MapEntry(1: 哈哈哈)嘻嘻嘻, MapEntry(2: 嘻嘻嘻)哈哈哈: MapEntry(2: 嘻嘻嘻)嘻嘻嘻},and type is _InternalLinkedHashMap<dynamic, dynamic>testMap8:{one: 4, two: 2, three: 3}, and type is _InternalLinkedHashMap<dynamic, dynamic>通过Map.of创建Map对象:{},and type is _InternalLinkedHashMap<dynamic, dynamic>通过Map.unmodifiable创建Map对象:{1: 哈哈哈, 2: 嘻嘻嘻},and type is UnmodifiableMapView<dynamic, dynamic>
testMap8的长度:4testMap8的key值(one, two, three, 哈哈哈)testMap8的value值:(4, 2, 3, 呸呸呸)testMap8是否为空:falsetestMap8是否为非空:truetestMap8的entries:(MapEntry(one: 4), MapEntry(two: 2), MapEntry(three: 3), MapEntry(哈哈哈: 呸呸呸)){one: 4, two: 2, three: 3, 哈哈哈: 呸呸呸, 1.235: 1.2222, 4: 4}testMap8的key中是否包含字符串4:falsetestMap8的key中是否包含one:truetestMap8的key中是否包含数字4:truetestMap8的value中是否包含字符串4:truetestMap8的value中是否包含数字4:truetestMap8中是否包含字符串哈哈:false使用addEntries添加数据之后的map:{one: 4, two: 2, three: 3, 哈哈哈: 呸呸呸, 1.235: 1.2222, 4: 4, 哼哼哼: 100, 咚咚咚: 22}
{1: 哈哈哈}
通过addAll添加数据之后的map:{one: 4, two: 2, three: 3, 哈哈哈: 呸呸呸, 1.235: 1.2222, 4: 4, 哼哼哼: 100, 咚咚咚: 22, 1: 哈哈哈}testCast:{one: 4, two: 2, three: 3, 哈哈哈: 呸呸呸, 1.235: 1.2222, 4: 4, 哼哼哼: 100, 咚咚咚: 22, 1: 哈哈哈}传入的key:one,传入的value4
传入的key:two,传入的value2
传入的key:three,传入的value3
传入的key:哈哈哈,传入的value呸呸呸
传入的key:1.235,传入的value1.2222
传入的key:4,传入的value4
传入的key:哼哼哼,传入的value100
传入的key:咚咚咚,传入的value22
传入的key:1,传入的value哈哈哈使用map对之前的Map进行操作,并返回一个新的Map:{newone: new4, newtwo: new2, newthree: new3, new哈哈哈: new呸呸呸, new1.235: new1.2222, new4: new4, new哼哼哼: new100, new咚咚咚: new22, new1: new哈哈哈},and testMapF is _InternalLinkedHashMap<dynamic, dynamic>{one: 4, two: 2, three: 3, 哈哈哈: 呸呸呸, 1.235: 1.2222, 4: 4, 哼哼哼: 100, 咚咚咚: 22, 1: 哈哈哈}
删除testMap8中key是int类型的元素:{one: 4, two: 2, three: 3, 哈哈哈: 呸呸呸, 1.235: 1.2222, 哼哼哼: 100, 咚咚咚: 22}更新一个map中存在的key的元素:{one: new4, two: 2, three: 3, 哈哈哈: 呸呸呸, 1.235: 1.2222, 哼哼哼: 100, 咚咚咚: 22}
not found key
更新一个map中不存在的元素并且指定ifAbsent参数:{one: new4, two: 2, three: 3, 哈哈哈: 呸呸呸, 1.235: 1.2222, 哼哼哼: 100, 咚咚咚: 22, 4: null}
自定义更新操作之后testMap8中的值:{one: null, two: null, three: null, 哈哈哈: null, 1.235: 2.2350000000000003, 哼哼哼: null, 咚咚咚: null, 4: 5}添加数据之前:{one: new4, two: 2, three: 3, 哈哈哈: 呸呸呸, 1.235: 2.2350000000000003, 哼哼哼: 100, 咚咚咚: 22, 4: 5}
not found key
添加数据之后{one: new4, two: 2, three: 3, 哈哈哈: 呸呸呸, 1.235: 2.2350000000000003, 哼哼哼: 100, 咚咚咚: 22, 4: 5, 哒哒哒: not found key}testMap8中的数据被清空之后: {}
Exited

Dart中Map的使用相关推荐

  1. 【Dart 教程系列第 28 篇】Dart中的 Map 和实体类的相互转换

    这是[Dart 教程系列第 28 篇],如果觉得有用的话,欢迎关注专栏. 首先声明一个实体类 class People {String name; // 姓名String area; // 地区Str ...

  2. 【Flutter】Dart 数据类型 Map 类型 ( 创建 Map 集合 | 初始化 Map 集合 | 遍历 Map 集合 )

    文章目录 一. Dart 数据类型 Map 类型 二. Map 类型初始化并赋值 1. 创建 Map 对象同时进行初始化操作 2. 先创建 Map 对象再进行赋值 三. Map 集合遍历 1. 使用 ...

  3. 详解Dart中如何通过注解生成代码

    简介:详解dart与java注解生成代码异同点 作者:闲鱼技术-龙湫 1.背景 最近在项目中使用到了Dart中的注解代码生成技术,这跟之前Java中APT+JavaPoet生成代码那套技术还是有一些不 ...

  4. 懂集合吗?对,是dart中的集合

    文章目录 简介 List的使用 Set的使用 Map的使用 常见的集合方法 总结 简介 dart中的集合有三个,分别是list,set和map.dart在dart:core包中提供了对于这三种集合非常 ...

  5. dart 遍历数组_flutter开发,Dart中的那些骚气语法!

    学习一门新的语言,我们可以以自己现有的熟悉的语言来类比,比如我们非常熟悉Java,那么剩下的就是需要掌握与Java不同的 国际惯例,使用Dart完成一个:"Hello,World!" ...

  6. C++ 中 map 的用法

    C++ 中 map 是一种键值对容器 初始化并赋值 map<string, int> mapAge = {{"张三", 19}, {"李四", 18 ...

  7. stream map方法_Java Stream中map和flatMap方法

    最近看到一篇讲stream语法的文章,学习Java中map()和flatMap()方法之间的区别. 虽然看起来这两种方法都做同样的事情,都是做的映射操作,但实际上差之毫厘谬以千里. 通过演示Demo中 ...

  8. Java中Map用法详解

    原文地址http://blog.csdn.net/guomutian911/article/details/45771621 原文地址http://blog.csdn.net/sunny2437885 ...

  9. 原 c++中map与unordered_map的区别

    c++中map与unordered_map的区别 头文件 map: #include < map > unordered_map: #include < unordered_map ...

最新文章

  1. 以下哪一个不属于python语言的特点-以下不属于python语言特点的是( )_学小易找答案...
  2. maven package自己主动部署包
  3. jcmd_jcmd,大约JDK 11
  4. 【Android OpenGL ES 开发 (二)】渲染管线与Shader
  5. Adobe illustrator 介绍几个快捷键 - 连载 8
  6. pager-taglib 使用说明
  7. CCF-CSP/202206-2—寻宝!大冒险!c++解题思路
  8. python迷宫地图代码_python实现的生成随机迷宫算法核心代码分享(含游戏完整代码)...
  9. 2018主流台式计算机跑分,pu天梯图2018最新版2018电脑cpu处理器性能排行榜
  10. 计算机app无法删除,文件夹删不掉怎么办?
  11. C++:Timer类实现
  12. 步态情绪识别:STEP学习
  13. 打台球百发百中?油管博主球杆上“做手脚”
  14. 创建PostgreSQL数据库
  15. mars3d学习-方量分析
  16. Linux关机shutdown命令解释
  17. games101:七,加速光线追踪(AABB、BVH、SAH)+ 作业6
  18. PhotoshopCS5无法使用扫描仪的故障
  19. 基于JavaEE的库存物资管理系统_JSP网站设计_SqlServer数据库设计
  20. 高三/高考前可以回家自学吗?——211学霸高三在家自学一年经验分享

热门文章

  1. MT6762 datasheet,MT6762规格书,MT6762芯片参数资料
  2. ShardingJDBC使用总结
  3. PDFBOX将PDF转图片(PNG背景透明)
  4. 鼓式制动系统行业研究及十四五规划分析报告
  5. vue中使用require动态获取图片地址
  6. mysql五日均线代码_5日均线--攻击线
  7. leetcode 1419 数青蛙
  8. 传说之下打开debug模式超超超超超超超超详细方法
  9. linux 未分配分区合并,这种情况如何扩展硬盘,合并硬盘分区,主分盘和未分配中间有个恢復分区,不能合并扩展硬盘!...
  10. Java输入某年某月某日,判断这一天是这一年的第几天?