{"id":205,"date":"2023-05-24T10:56:28","date_gmt":"2023-05-24T02:56:28","guid":{"rendered":"https:\/\/www.escook.cn\/?p=205"},"modified":"2023-09-26T21:06:21","modified_gmt":"2023-09-26T13:06:21","slug":"dart-%e5%9f%ba%e7%a1%80-%e5%86%85%e7%bd%ae%e7%b1%bb%e5%9e%8b-map-%e9%9b%86%e5%90%88","status":"publish","type":"post","link":"https:\/\/www.escook.cn\/index.php\/2023\/05\/24\/dart-%e5%9f%ba%e7%a1%80-%e5%86%85%e7%bd%ae%e7%b1%bb%e5%9e%8b-map-%e9%9b%86%e5%90%88\/","title":{"rendered":"Dart \u57fa\u7840 \u2013 \u5185\u7f6e\u7c7b\u578b \u2013 Map \u96c6\u5408"},"content":{"rendered":"<blockquote>\n<p>\u7248\u6743\u5f52\u4f5c\u8005 <strong>\u00a9\u5218\u9f99\u5bbe<\/strong> \u6240\u6709\uff0c\u672c\u6587\u7ae0\u672a\u7ecf\u4f5c\u8005\u5141\u8bb8\uff0c\u7981\u6b62\u79c1\u81ea\u8f6c\u8f7d\uff01<\/p>\n<\/blockquote>\n<p>Map \u662f\u4e00\u4e2a\u65e0\u5e8f\u7684 <code>key-value<\/code> \uff08\u952e\u503c\u5bf9\uff09\u96c6\u5408\uff0c\u5c31\u662f\u5927\u5bb6\u719f\u77e5\u7684 <strong>dictionary<\/strong> \u6216\u8005 <strong>hash<\/strong>\u3002 Map \u5c06 kay \u4e0e value \u5173\u8054\uff0c\u4ee5\u4fbf\u4e8e\u68c0\u7d22\u3002<\/p>\n<p>\u901a\u5e38\u6765\u8bf4\uff0cMap \u662f\u7528\u6765\u5173\u8054 <code>keys<\/code> \u548c <code>values<\/code> \u7684\u5bf9\u8c61\u3002\u5176\u4e2d\u952e\u548c\u503c\u90fd\u53ef\u4ee5\u662f<strong>\u4efb\u4f55\u7c7b\u578b<\/strong>\u7684\u5bf9\u8c61\u3002\u6bcf\u4e2a<strong>\u952e<\/strong>\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\uff0c\u4f46\u662f<strong>\u503c<\/strong>\u53ef\u4ee5\u91cd\u590d\u51fa\u73b0\u591a\u6b21\u3002<\/p>\n<h2>1. \u521b\u5efa Map \u96c6\u5408<\/h2>\n<h3>1.1 \u57fa\u4e8e\u5b57\u9762\u91cf\u521b\u5efa Map<\/h3>\n<h4>1.1.1 \u5f3a\u7c7b\u578b\u7684 Map<\/h4>\n<p>\u58f0\u660e\u5f3a\u7c7b\u578b Map \u7684\u8bed\u6cd5\u683c\u5f0f\u4e3a <code>&lt;keyType, valueType&gt;{}<\/code>\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u521b\u5efa\u7a7a\u7684 Map \u96c6\u5408\n  \/\/ \u952e\u662f\u5b57\u7b26\u4e32\uff0c\u503c\u662f\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\n  var hobbies1 = &lt;String, List&lt;String&gt;&gt;{};\n  \/\/ \u8f93\u51fa true\uff0c\u8bc1\u660e hobbies1 \u662f\u7a7a Map\n  print(hobbies1.isEmpty);\n\n  \/\/ 2. \u521b\u5efa\u975e\u7a7a\u7684 Map \u96c6\u5408\n  \/\/ \u952e\u662f\u5b57\u7b26\u4e32\uff0c\u503c\u662f\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\n  var hobbies2 = &lt;String, List&lt;String&gt;&gt;{\n    &#039;liulongbin&#039;: [&#039;games&#039;, &#039;coding&#039;, &#039;music&#039;],\n    &#039;escook&#039;: [&#039;eat&#039;, &#039;sleep&#039;, &#039;run&#039;]\n  };\n  \/\/ \u8f93\u51fa 2\uff0c\u8bc1\u660e hobbies2 \u4e2d\u5305\u542b\u4e24\u4e2a\u952e\u503c\u5bf9\n  print(hobbies2.length);\n}<\/code><\/pre>\n<h4>1.1.2 \u5f31\u7c7b\u578b\u7684 Map<\/h4>\n<p>\u76f4\u63a5\u4f7f\u7528 <code>{}<\/code> \u5b57\u9762\u91cf\u5b9a\u4e49\u7684\u5c31\u662f\u5f31\u7c7b\u578b\u7684 Map\uff0c\u5b83\u7684\u5b8c\u6574\u5199\u6cd5\u662f <code>&lt;dynamic, dynamic&gt;{}<\/code>\u3002\u5f31\u7c7b\u578b Map \u7684\u597d\u5904\u662f\u53ef\u4ee5\u5411 Map \u96c6\u5408\u4e2d\u6dfb\u52a0\u4efb\u610f\u7c7b\u578b\u7684\u952e\u503c\u5bf9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u58f0\u660e\u952e\u548c\u503c\u90fd\u4e3a dynamic \u7c7b\u578b\u7684 Map\uff0c\n  \/\/ \u5b8c\u6574\u5199\u6cd5\u662f var maps = &lt;dynamic, dynamic&gt;{};\n  var maps = {};\n\n  \/\/ 2. \u5411 Map \u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9\n  \/\/ \u952e\u662f String\uff0c\u503c\u662f List&lt;String&gt;\n  maps[&#039;liulongbin&#039;] = [&#039;games&#039;, &#039;coding&#039;, &#039;music&#039;];\n  \/\/ \u952e\u662f int\uff0c\u503c\u662f String\n  maps[1] = &#039;hello&#039;;\n  \/\/ \u952e\u662f int\uff0c\u503c\u662f String\n  maps[2] = &#039;world&#039;;\n\n  \/\/ 3. \u8f93\u51fa {liulongbin: [games, coding, music], 1: hello, 2: world}\n  print(maps);\n}<\/code><\/pre>\n<h4>1.1.3 \u5f31\u7c7b\u578b\u7684\u952e\u6216\u5f31\u7c7b\u578b\u7684\u503c<\/h4>\n<p>\u5728\u4f7f\u7528 Map \u65f6\uff0c\u8fd8\u53ef\u4ee5\u5355\u72ec\u6307\u5b9a\u5f31\u7c7b\u578b\u7684\u952e\uff0c\u6216\u5355\u72ec\u6307\u5b9a\u5f31\u7c7b\u578b\u7684\u503c\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49\u5f31\u7c7b\u578b\u7684\u952e\uff0c\u5f3a\u7c7b\u578b\u7684\u503c\n  var map1 = &lt;dynamic, String&gt;{};\n  \/\/ 2. \u5b9a\u4e49\u5f3a\u7c7b\u578b\u7684\u952e\uff0c\u5f31\u7c7b\u578b\u7684\u503c\n  var map2 = &lt;int, dynamic&gt;{};\n\n  \/\/ 1.1 \u5411 map1 \u4e2d\u6dfb\u52a0\u4e09\u4e2a\u952e\u503c\u5bf9\n  map1[0] = &#039;red&#039;;\n  map1[true] = &#039;blue&#039;;\n  map1[&#039;flag&#039;] = &#039;green&#039;;\n\n  \/\/ 1.2 \u8f93\u51fa {0: red, true: blue, flag: green}\n  print(map1);\n\n  \/\/ 2.1 \u5411 map2 \u4e2d\u6dfb\u52a0\u56db\u4e2a\u952e\u503c\u5bf9\n  map2[0] = &#039;red&#039;;\n  map2[1] = false;\n  map2[2] = 999;\n  map2[3] = &lt;String&gt;[&#039;A&#039;, &#039;B&#039;, &#039;C&#039;];\n\n  \/\/ 2.2 \u8f93\u51fa {0: red, 1: false, 2: 999, 3: [A, B, C]}\n  print(map2);\n}<\/code><\/pre>\n<h3>1.2 \u57fa\u4e8e\u6784\u9020\u51fd\u6570\u521b\u5efa Map<\/h3>\n<p>\u57fa\u4e8e <code>Map()<\/code> \u6784\u9020\u51fd\u6570\u540c\u6837\u53ef\u4ee5\u521b\u5efa\u5f31\u7c7b\u578b\u548c\u5f3a\u7c7b\u578b\u7684 Map\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u521b\u5efa\u5f31\u7c7b\u578b\u7684\u7a7a Map\n  var map1 = Map();\n  \/\/ 2. \u521b\u5efa\u5f3a\u7c7b\u578b\u7684\u7a7a Map\n  var map2 = Map&lt;int, String&gt;();\n}<\/code><\/pre>\n<h2>2. \u64cd\u4f5c Map \u5143\u7d20<\/h2>\n<h3>2.1 \u6dfb\u52a0\u5143\u7d20<\/h3>\n<h4>2.1.1 \u76f4\u63a5\u4f7f\u7528 [] \u6dfb\u52a0\u5143\u7d20<\/h4>\n<p>\u5411 Map \u96c6\u5408\u4e2d\u6dfb\u52a0\u5143\u7d20\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f <code>[]<\/code>,\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49\u7a7a Map\n  var names = &lt;int, String&gt;{};\n  \/\/ 2. \u6dfb\u52a0\u5143\u7d20\n  names[0] = &#039;liulongbin&#039;;\n  \/\/ 3. \u8f93\u51fa {0: liulongbin}\n  print(names);\n}<\/code><\/pre>\n<h4>2.1.2 addAll<\/h4>\n<p>\u8fd8\u53ef\u4ee5\u8c03\u7528 <code>addAll<\/code> \u65b9\u6cd5\uff0c\u628a\u6307\u5b9a Map \u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u5f53\u524d Map \u4e2d\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49\u4e24\u4e2a Map\n  var names = &lt;int, String&gt;{0: &#039;liulongbin&#039;, 1: &#039;escook&#039;};\n  var others = &lt;int, String&gt;{1: &#039;zs&#039;, 2: &#039;ls&#039;, 3: &#039;xyz&#039;};\n\n  \/\/ 2. \u628a others \u4e2d\u7684\u952e\u503c\u5bf9\u6dfb\u52a0\u5230 names \u4e2d\n  \/\/ names \u548c others \u91cd\u590d\u7684\u952e\uff0c\u4f1a\u88ab others \u4e2d\u5bf9\u5e94\u7684\u503c\u8986\u76d6\n  names.addAll(others);\n  \/\/ 3. \u8f93\u51fa {0: liulongbin, 1: zs, 2: ls, 3: xyz}\n  print(names);\n}<\/code><\/pre>\n<h3>2.2 \u4fee\u6539\u5143\u7d20<\/h3>\n<h4>2.2.1 \u76f4\u63a5\u4f7f\u7528 [] \u4fee\u6539<\/h4>\n<p>\u4fee\u6539\u5143\u7d20\u7684\u503c\uff0c\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f\u5229\u7528 <code>[]<\/code> \u4e3a\u6307\u5b9a\u7684\u952e\u91cd\u65b0\u8d4b\u503c\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  var names = &lt;String, int&gt;{&#039;liulongbin&#039;: 18};\n  \/\/ \u8f93\u51fa {liulongbin: 18}\n  print(names);\n\n  \/\/ \u4f7f\u7528 [] \u4e3a\u6307\u5b9a\u7684 key \u91cd\u65b0\u8d4b\u503c\n  names[&#039;liulongbin&#039;] = 20;\n  \/\/ \u8f93\u51fa {liulongbin: 20}\n  print(names);\n}<\/code><\/pre>\n<h4>2.2.2 update<\/h4>\n<p>\u8c03\u7528 Map \u5bf9\u8c61\u7684 <code>update<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u65b0\u6307\u5b9a key \u7684\u503c\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49 Map \u5bf9\u8c61\n  var names = &lt;String, int&gt;{&#039;liulongbin&#039;: 18};\n\n  \/\/ 2. \u8c03\u7528 update \u65b9\u6cd5\uff0c\n  \/\/ \u53c2\u65701\uff1a\u8981\u66f4\u65b0\u7684 key\n  \/\/ \u53c2\u65702\uff1a\u533f\u540d\u51fd\u6570\uff0c\u5f62\u53c2 value \u662f key \u5f53\u524d\u5bf9\u5e94\u7684\u503c\uff0c\u51fd\u6570\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u503c\u5f53\u4f5c key \u7684\u65b0\u503c\n  \/\/ \u8fd4\u56de\u503c\uff1a\u66f4\u65b0\u540e\u7684\u65b0\u503c\n  var result = names.update(&#039;liulongbin&#039;, (value) =&gt; value + 1);\n\n  \/\/ 3. \u8f93\u51fa {liulongbin: 19}\n  print(names);\n  \/\/ 4. \u8f93\u51fa 19\n  print(result);\n}<\/code><\/pre>\n<p>\u540c\u65f6 <code>update<\/code> \u51fd\u6570\u8fd8\u63a5\u6536\u7b2c\u4e09\u4e2a\u53c2\u6570 <code>ifAbsent<\/code>\uff0c\u5b83\u662f\u4e00\u4e2a<strong>\u533f\u540d\u51fd\u6570<\/strong>\uff0c\u7528\u6765\u63d0\u4f9b\u4e00\u4e2a<strong>\u7f3a\u7701\u7684\u5019\u8865\u503c<\/strong>\u3002\u5982\u679c\u5728\u66f4\u65b0\u5c5e\u6027\u503c\u7684\u65f6\u5019\u53d1\u73b0 Map \u4e2d\u4e0d\u5305\u542b\u5f53\u524d key\uff0c\u5219\u4e3a\u5f53\u524d Map \u65b0\u589e key \u5c5e\u6027\uff0c\u5c5e\u6027\u503c\u5c31\u662f\u8fd9\u4e2a\u5019\u8865\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49 Map \u5bf9\u8c61\n  var names = &lt;String, int&gt;{&#039;liulongbin&#039;: 18};\n\n  \/\/ 2. \u8c03\u7528 update \u65b9\u6cd5\uff0c\n  \/\/ \u53c2\u65701\uff1a\u8981\u66f4\u65b0\u7684 key\n  \/\/ \u53c2\u65702\uff1a\u533f\u540d\u51fd\u6570\uff0c\u5f62\u53c2 value \u662f key \u5f53\u524d\u5bf9\u5e94\u7684\u503c\uff0c\u51fd\u6570\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u503c\u5f53\u4f5c key \u7684\u65b0\u503c\n  \/\/ \u53c2\u65703\uff1a\u5019\u8865\u503c\uff0c\u5982\u679c key \u4e0d\u5b58\u5728\uff0c\u5219\u4e3a Map \u65b0\u589e key \u5c5e\u6027\uff0c\u5c5e\u6027\u503c\u662f\u5f53\u524d\u7684\u5019\u8865\u503c\n  \/\/ \u8fd4\u56de\u503c\uff1a\u66f4\u65b0\u540e\u7684\u65b0\u503c\n  var result = names.update(&#039;escook&#039;, (value) =&gt; value + 1, ifAbsent: () =&gt; 0);\n\n  \/\/ 3. \u8f93\u51fa {liulongbin: 18, escook: 0}\n  \/\/ \u7531\u4e8e names \u5bf9\u8c61\u4e2d\u539f\u672c\u4e0d\u5305\u542b escook \u5c5e\u6027\uff0c\n  \/\/ \u6240\u4ee5 update \u65b9\u6cd5\u4e3a names \u65b0\u589e\u4e86 escook \u5c5e\u6027\uff0c\u5c5e\u6027\u503c\u4e3a\u53c2\u65703\u63d0\u4f9b\u7684\u5019\u8865\u503c\n  print(names);\n  \/\/ 4. \u8f93\u51fa 0\n  print(result);\n}<\/code><\/pre>\n<h4>2.2.3 updateAll<\/h4>\n<p><code>updateAll<\/code> \u53ef\u4ee5\u6279\u91cf\u66f4\u65b0 Map \u4e2d\u7684\u6bcf\u4e2a\u503c\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49 Map\n  var scores = &lt;String, String&gt;{&#039;liulongbin&#039;: &#039;A&#039;, &#039;escook&#039;: &#039;D&#039;};\n\n  \/\/ 2. \u5728\u6bcf\u4e2a\u5c5e\u6027\u503c\u7684\u540e\u9762\u6dfb\u52a0 + \u540e\u7f00\n  scores.updateAll((key, value) =&gt; &#039;$value+&#039;);\n\n  \/\/ 3. \u8f93\u51fa {liulongbin: A+, escook: D+}\n  print(scores);\n}<\/code><\/pre>\n<h3>2.3 \u5220\u9664\u5143\u7d20<\/h3>\n<h4>2.3.1 remove<\/h4>\n<p><code>remove<\/code> \u65b9\u6cd5\u6839\u636e key \u4ece Map \u4e2d\u79fb\u9664\u952e\u503c\u5bf9\uff0c<strong>\u5e76\u8fd4\u56de\u79fb\u9664\u7684 value \u503c<\/strong>\u3002\u5982\u679c\u79fb\u9664\u7684 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de <code>null<\/code>\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49 Map\n  var scores = &lt;String, String&gt;{&#039;liulongbin&#039;: &#039;A&#039;, &#039;escook&#039;: &#039;D&#039;};\n\n  \/\/ 2. \u79fb\u9664 escook \u952e\u4ee5\u53ca\u5bf9\u5e94\u7684\u503c\n  var result = scores.remove(&#039;escook&#039;);\n\n  \/\/ 3.1 \u8f93\u51fa D\n  print(result);\n  \/\/ 3.2 \u8f93\u51fa {liulongbin: A}\n  print(scores);\n\n  \/\/ ----\n\n  \/\/ 4. \u79fb\u9664\u4e0d\u5b58\u5728\u7684\u5c5e\u6027 abc\n  var result2 = scores.remove(&#039;abc&#039;);\n\n  \/\/ 5. \u8f93\u51fa null\n  print(result2);\n}<\/code><\/pre>\n<h4>2.3.2 removeWhere<\/h4>\n<p><code>removeWhere<\/code> \u53ef\u4ee5\u6839\u636e\u7ed9\u5b9a\u7684\u6761\u4ef6\uff0c\u79fb\u9664\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49 Map\n  var scores = &lt;String, String&gt;{\n    &#039;liulongbin&#039;: &#039;A&#039;,\n    &#039;escook&#039;: &#039;D&#039;,\n    &#039;zs&#039;: &#039;C&#039;,\n    &#039;ls&#039;: &#039;D&#039;\n  };\n\n  \/\/ 2. \u79fb\u9664\u5206\u6570\u4e3a D \u7684\u5143\u7d20\uff0c\u6216 key \u4e3a &#039;zs&#039; \u7684\u5143\u7d20\n  scores.removeWhere((key, value) =&gt; value == &#039;D&#039; || key == &#039;zs&#039;);\n\n  \/\/ 3. \u8f93\u51fa {liulongbin: A}\n  print(scores);\n}<\/code><\/pre>\n<h3>2.4 \u6e05\u7a7a\u5143\u7d20<\/h3>\n<p><code>clear<\/code> \u65b9\u6cd5\u53ef\u4ee5\u6e05\u7a7a Map \u96c6\u5408\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49 Map\n  var scores = &lt;String, String&gt;{&#039;liulongbin&#039;: &#039;A&#039;, &#039;escook&#039;: &#039;D&#039;};\n  \/\/ 2. \u6e05\u7a7a\u6240\u6709\u952e\u503c\u5bf9\n  scores.clear();\n  \/\/ 3. \u8f93\u51fa true\uff0c\u8bc1\u660e scores \u5df2\u4e3a\u7a7a\n  print(scores.isEmpty);\n}<\/code><\/pre>\n<h2>3. Map \u5e38\u7528\u65b9\u6cd5<\/h2>\n<h3>3.1 containsKey<\/h3>\n<p><code>containsKey<\/code> \u7528\u6765\u5224\u65ad Map \u4e2d\u662f\u5426\u5305\u542b\u6307\u5b9a\u7684 key\uff0c\u5982\u679c\u5305\u542b\u5219\u8fd4\u56de true\uff0c\u5982\u679c\u4e0d\u5305\u542b\u5219\u8fd4\u56de false\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49 Map\n  var scores = &lt;String, String&gt;{\n    &#039;liulongbin&#039;: &#039;A&#039;,\n    &#039;escook&#039;: &#039;D&#039;,\n    &#039;zs&#039;: &#039;C&#039;,\n    &#039;ls&#039;: &#039;D&#039;\n  };\n\n  var res1 = scores.containsKey(&#039;escook&#039;);\n  print(res1); \/\/ \u8f93\u51fa true\n  var res2 = scores.containsKey(&#039;nbb&#039;);\n  print(res2); \/\/ \u8f93\u51fa false\n}<\/code><\/pre>\n<h3>3.2 containsValue<\/h3>\n<p><code>containsValue<\/code> \u7528\u6765\u5224\u65ad Map \u4e2d\u662f\u5426\u5305\u542b\u6307\u5b9a\u7684 value\uff0c\u5982\u679c\u5305\u542b\u5219\u8fd4\u56de true\uff0c\u5982\u679c\u4e0d\u5305\u542b\u5219\u8fd4\u56de false\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49 Map\n  var scores = &lt;String, String&gt;{\n    &#039;liulongbin&#039;: &#039;A&#039;,\n    &#039;escook&#039;: &#039;D&#039;,\n    &#039;zs&#039;: &#039;C&#039;,\n    &#039;ls&#039;: &#039;D&#039;\n  };\n\n  var res1 = scores.containsValue(&#039;A&#039;);\n  print(res1); \/\/ \u8f93\u51fa true\n  var res2 = scores.containsValue(&#039;B&#039;);\n  print(res2); \/\/ \u8f93\u51fa false\n}<\/code><\/pre>\n<h3>3.3 putIfAbsent<\/h3>\n<p><code>putIfAbsent<\/code> \u65b9\u6cd5\u4f1a\u4e3a\u5bf9\u8c61<strong>\u6dfb\u52a0\u539f\u672c\u4e0d\u5b58\u5728\u7684\u5c5e\u6027\uff0c\u5e76\u4e3a\u5176\u8d4b\u503c<\/strong>\u3002\u5982\u679c\u8981\u6dfb\u52a0\u7684<strong>\u5c5e\u6027\u5df2\u5b58\u5728<\/strong>\uff0c\u5219<strong>\u4e0d\u4f1a\u8986\u76d6<\/strong>\uff0c<strong>\u4ec5\u628a<\/strong>\u6b64\u5c5e\u6027\u7684<strong>\u539f\u503c\u8fd4\u56de<\/strong>\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49 Map\n  var scores = &lt;String, String&gt;{&#039;liulongbin&#039;: &#039;A&#039;, &#039;escook&#039;: &#039;D&#039;};\n\n  \/\/ 2. \u68c0\u6d4b\u5230 scores \u4e2d\u5df2\u5305\u542b escook \u5c5e\u6027\uff0c\n  \/\/ \u56e0\u6b64\u4e0d\u4f1a\u91cd\u65b0\u8bbe\u7f6e escook \u5c5e\u6027\u7684\u503c\uff0c\u53ea\u662f\u5355\u7eaf\u7684\u628a\u539f\u5c5e\u6027\u503c &#039;D&#039; \u8fd4\u56de\u7ed9\u53d8\u91cf result\n  var result = scores.putIfAbsent(&#039;escook&#039;, () =&gt; &#039;C&#039;);\n\n  \/\/ 3.1 \u8f93\u51fa {liulongbin: A, escook: D}\uff0c\u6ca1\u6709\u4efb\u4f55\u53d8\u5316\n  print(scores);\n  \/\/ 3.2 \u8f93\u51fa D\uff0c\u539f\u503c\n  print(result);\n\n  \/\/ ----\n\n  \/\/ 4. \u68c0\u6d4b\u5230 scores \u4e2d\u4e0d\u5305\u542b llb \u5c5e\u6027\uff0c\n  \/\/ \u56e0\u6b64\u4f1a\u7ed9 scores \u5bf9\u8c61\u6dfb\u52a0\u65b0\u5c5e\u6027 llb\uff0c\u5c5e\u6027\u503c\u4e3a\u5b57\u7b26\u4e32 A+\n  var result2 = scores.putIfAbsent(&#039;llb&#039;, () =&gt; &#039;A+&#039;);\n  \/\/ 5.1 \u8f93\u51fa {liulongbin: A, escook: D, llb: A+}\n  print(scores);\n  \/\/ 5.2 \u8f93\u51fa A+\n  print(result2);\n}<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u5982\u679c\u60a8\u60f3<strong>\u8986\u76d6\u539f\u5c5e\u6027\u503c<\/strong>\u6216<strong>\u6dfb\u52a0\u539f\u672c\u6ca1\u6709\u7684\u65b0\u5c5e\u6027<\/strong>\uff0c\u5efa\u8bae\u4f7f\u7528 <code>update<\/code> \u65b9\u6cd5\u6216\u4f7f\u7528 <code>[]<\/code> \u7684\u65b9\u5f0f\u3002<\/p>\n<\/blockquote>\n<h3>3.4 forEach<\/h3>\n<p><code>forEach<\/code> \u65b9\u6cd5\u53ef\u4ee5\u904d\u5386\u5f53\u524d Map \u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\uff0c\u5e76\u5bf9\u4ed6\u4eec\u8fdb\u884c\u81ea\u5b9a\u4e49\u7684\u64cd\u4f5c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-dart\">void main(List&lt;String&gt; args) {\n  \/\/ 1. \u5b9a\u4e49 Map\n  var scores = &lt;String, String&gt;{&#039;liulongbin&#039;: &#039;A&#039;, &#039;escook&#039;: &#039;D&#039;};\n\n  \/\/ 2. \u904d\u5386 scores \u4e2d\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\n  scores.forEach((key, value) {\n    print(&#039;\u5b66\u751f $key \u7684\u6210\u7ee9\u4e3a $value&#039;);\n  });\n}<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\u610f\uff1aList \u6570\u7ec4\u3001Set \u96c6\u5408\u3001Map \u90fd\u652f\u6301 forEach \u65b9\u6cd5\u5bf9\u8c61\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u904d\u5386\u64cd\u4f5c\uff0c\u800c\u4e14\u7528\u6cd5\u76f8\u540c\u3002<\/p>\n<\/blockquote>\n<p>Map \u53ef\u4ee5\u50cf List \u4e00\u6837\u652f\u6301\u4f7f\u7528\u6269\u5c55\u64cd\u4f5c\u7b26\uff08<code>...<\/code> \u548c <code>...?<\/code>\uff09\u4ee5\u53ca\u96c6\u5408\u7684 <code>if<\/code> \u548c <code>for<\/code> \u64cd\u4f5c\u3002\u5173\u4e8e <code>Map <\/code> \u96c6\u5408\u7684\u66f4\u591a\u64cd\u4f5c\uff0c\u8bf7\u53c2\u8003 <a href=\"https:\/\/api.dart.cn\/stable\/2.18.5\/dart-core\/Map-class.html\">Map \u7684\u5b98\u65b9\u6587\u6863<\/a><\/p>\n<blockquote>\n<p>\u7248\u6743\u5f52\u4f5c\u8005 <strong>\u00a9\u5218\u9f99\u5bbe<\/strong> \u6240\u6709\uff0c\u672c\u6587\u7ae0\u672a\u7ecf\u4f5c\u8005\u5141\u8bb8\uff0c\u7981\u6b62\u79c1\u81ea\u8f6c\u8f7d\uff01\u3002<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u7248\u6743\u5f52\u4f5c\u8005 \u00a9\u5218\u9f99\u5bbe \u6240\u6709\uff0c\u672c\u6587\u7ae0\u672a\u7ecf\u4f5c\u8005\u5141\u8bb8\uff0c\u7981\u6b62\u79c1\u81ea\u8f6c\u8f7d\uff01 Map \u662f\u4e00\u4e2a\u65e0\u5e8f\u7684 key-value \uff08\u952e\u503c\u5bf9\uff09\u96c6\u5408\uff0c\u5c31\u662f\u5927\u5bb6\u719f\u77e5\u7684 dictionary \u6216\u8005 hash\u3002 Map \u5c06 kay \u4e0e value \u5173\u8054\uff0c\u4ee5\u4fbf\u4e8e\u68c0\u7d22\u3002 \u901a\u5e38\u6765\u8bf4\uff0cMap \u662f\u7528\u6765\u5173\u8054 keys \u548c values \u7684\u5bf9\u8c61\u3002\u5176\u4e2d\u952e\u548c\u503c\u90fd\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u6bcf\u4e2a\u952e\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\uff0c\u4f46\u662f\u503c\u53ef\u4ee5\u91cd\u590d\u51fa\u73b0\u591a\u6b21\u3002 1. \u521b\u5efa Map \u96c6\u5408 1.1 \u57fa\u4e8e\u5b57\u9762\u91cf\u521b\u5efa Map 1.1.1 \u5f3a\u7c7b\u578b\u7684 Map \u58f0\u660e\u5f3a\u7c7b\u578b Map \u7684\u8bed\u6cd5\u683c\u5f0f\u4e3a &lt;keyType, valueType&gt;{}\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a void main(List&lt;String&gt; args) { \/\/ 1. \u521b\u5efa\u7a7a\u7684 Map \u96c6\u5408 \/\/ \u952e\u662f\u5b57\u7b26\u4e32\uff0c\u503c\u662f\u5b57\u7b26\u4e32\u7684\u6570\u7ec4 var hobbies1 = &lt;String, List&lt;String&gt;&gt;{}; \/\/ \u8f93\u51fa true\uff0c\u8bc1\u660e hobbies1 \u662f\u7a7a Map print(hobbies1.isEmpty); \/\/ 2. \u521b\u5efa\u975e\u7a7a\u7684 Map \u96c6\u5408 \/\/ \u952e\u662f\u5b57\u7b26\u4e32\uff0c\u503c\u662f\u5b57\u7b26\u4e32\u7684\u6570\u7ec4 var hobbies2 = &lt;String, List&lt;String&gt;&gt;{ &#039;liulongbin&#039;: [&#039;games&#039;, &#039;coding&#039;, &#039;music&#039;], &#039;escook&#039;: [&#039;eat&#039;, &#039;sleep&#039;, &#039;run&#039;] }; \/\/ \u8f93\u51fa 2\uff0c\u8bc1\u660e hobbies2 \u4e2d\u5305\u542b\u4e24\u4e2a\u952e\u503c\u5bf9 print(hobbies2.length); } 1.1.2 \u5f31\u7c7b\u578b\u7684 Map \u76f4\u63a5\u4f7f\u7528 {} \u5b57\u9762\u91cf\u5b9a\u4e49\u7684\u5c31\u662f\u5f31\u7c7b\u578b\u7684 Map\uff0c\u5b83\u7684\u5b8c\u6574\u5199\u6cd5\u662f &lt;dynamic, dynamic&gt;{}\u3002\u5f31\u7c7b\u578b Map \u7684\u597d\u5904\u662f\u53ef\u4ee5\u5411 Map \u96c6\u5408\u4e2d\u6dfb\u52a0\u4efb\u610f\u7c7b\u578b\u7684\u952e\u503c\u5bf9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a void main(List&lt;String&gt; args) { \/\/ 1. \u58f0\u660e\u952e\u548c\u503c\u90fd\u4e3a dynamic \u7c7b\u578b\u7684 Map\uff0c \/\/ \u5b8c\u6574\u5199\u6cd5\u662f var maps = &lt;dynamic, dynamic&gt;{}; var maps = {}; \/\/ 2. \u5411 Map \u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 \/\/ \u952e\u662f String\uff0c\u503c\u662f List&lt;String&gt; maps[&#039;liulongbin&#039;] = [&#039;games&#039;, &#039;coding&#039;, &#039;music&#039;]; \/\/ \u952e\u662f int\uff0c\u503c\u662f String maps[1] = &#039;hello&#039;; \/\/ \u952e\u662f int\uff0c\u503c\u662f String maps[2] = &#039;world&#039;; \/\/ 3. \u8f93\u51fa {liulongbin: [games, coding, music], 1: hello, 2: world} print(maps); } 1.1.3 \u5f31\u7c7b\u578b\u7684\u952e\u6216\u5f31\u7c7b\u578b\u7684\u503c \u5728\u4f7f\u7528 Map \u65f6\uff0c\u8fd8\u53ef\u4ee5\u5355\u72ec\u6307\u5b9a\u5f31\u7c7b\u578b\u7684\u952e\uff0c\u6216\u5355\u72ec\u6307\u5b9a\u5f31\u7c7b\u578b\u7684\u503c\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a void main(List&lt;String&gt; args) { \/\/ 1. \u5b9a\u4e49\u5f31\u7c7b\u578b\u7684\u952e\uff0c\u5f3a\u7c7b\u578b\u7684\u503c var map1 = &lt;dynamic, String&gt;{}; \/\/ 2. \u5b9a\u4e49\u5f3a\u7c7b\u578b\u7684\u952e\uff0c\u5f31\u7c7b\u578b\u7684\u503c var map2 = &lt;int, dynamic&gt;{}; \/\/ 1.1 \u5411 map1 \u4e2d\u6dfb\u52a0\u4e09\u4e2a\u952e\u503c\u5bf9 map1[0] = &#039;red&#039;; map1[true] = &#039;blue&#039;; map1[&#039;flag&#039;] = &#039;green&#039;; \/\/ 1.2 \u8f93\u51fa {0: red, true: blue, flag: green} print(map1); \/\/ 2.1 \u5411 map2 \u4e2d\u6dfb\u52a0\u56db\u4e2a\u952e\u503c\u5bf9 map2[0] = &#039;red&#039;; map2[1] = false; map2[2] = 999; map2[3] = &lt;String&gt;[&#039;A&#039;, &#039;B&#039;, &#039;C&#039;]; \/\/ 2.2 \u8f93\u51fa {0: red, 1: false, 2: 999, 3: [A, B, C]} print(map2); } 1.2 \u57fa\u4e8e\u6784\u9020\u51fd\u6570\u521b\u5efa Map \u57fa\u4e8e Map() \u6784\u9020\u51fd\u6570\u540c\u6837\u53ef\u4ee5\u521b\u5efa\u5f31\u7c7b\u578b\u548c\u5f3a\u7c7b\u578b\u7684&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[9],"class_list":["post-205","post","type-post","status-publish","format-standard","hentry","category-dart","tag-dart"],"_links":{"self":[{"href":"https:\/\/www.escook.cn\/index.php\/wp-json\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.escook.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.escook.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.escook.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.escook.cn\/index.php\/wp-json\/wp\/v2\/comments?post=205"}],"version-history":[{"count":2,"href":"https:\/\/www.escook.cn\/index.php\/wp-json\/wp\/v2\/posts\/205\/revisions"}],"predecessor-version":[{"id":575,"href":"https:\/\/www.escook.cn\/index.php\/wp-json\/wp\/v2\/posts\/205\/revisions\/575"}],"wp:attachment":[{"href":"https:\/\/www.escook.cn\/index.php\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.escook.cn\/index.php\/wp-json\/wp\/v2\/categories?post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.escook.cn\/index.php\/wp-json\/wp\/v2\/tags?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}