{"id":2233,"date":"2023-05-07T06:30:11","date_gmt":"2023-05-06T22:30:11","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2233"},"modified":"2023-05-07T09:15:13","modified_gmt":"2023-05-07T01:15:13","slug":"groovy-code-example-map","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/05\/07\/groovy-code-example-map\/","title":{"rendered":"Groovy\u4ee3\u7801\u793a\u4f8b &#8211; map"},"content":{"rendered":"<h2>map\u7684\u5b9a\u4e49<\/h2>\n<p>map\u4e00\u822c\u7528\u4e8ekey-value\u952e\u503c\u5bf9\u7684\u5b58\u653e\u3002\u5728groovy\u4e2d\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u65b9\u5f0f\u5b9a\u4e49map\uff1a<\/p>\n<ul>\n<li>1\uff09\u5b9a\u4e49\u4e00\u4e2a\u7a7amap\u7136\u540e\u518d\u5b58\u653ekey-value\u952e\u503c<\/li>\n<li>2\uff09\u76f4\u63a5\u5b9a\u4e49\u5e26\u6709\u521d\u59cb\u503c\u7684map<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u5bf9\u6bd4\u4e00\u4e0b\u4e24\u79cd\u65b9\u5f0f\u7684\u5dee\u522b\u548c\u7528\u6cd5\u3002<\/p>\n<pre><code class=\"language-java\">\/\/\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u7684map\ndef emptyStudentMap = [:]\nprintln emptyStudentMap \/\/ [:]\n\n\/\/\u5b9a\u4e49\u5e26\u6709\u521d\u59cb\u503c\u7684map\ndef studentMapWithInitialValue = [name: &quot;John&quot;, surname: &quot;Doe&quot;, age: 17]\nprintln studentMapWithInitialValue \/\/ [name:John, surname:Doe, age:17]<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f<code>[]<\/code>\u4e2d\u7684\u5192\u53f7<code>:<\/code>\uff0c\u8868\u660e\u8fd9\u662f\u4e00\u4e2amap\u800c\u4e0d\u662flist<\/p>\n<p>\u800c\u5bf9\u4e8emap\u4e2d\u7684\u521d\u59cb\u503c\uff0ckey\u503c\u662f\u4e0d\u9700\u8981\u53cc\u5f15\u53f7<code>&quot;&quot;<\/code>\u7684\uff0c\u9ed8\u8ba4\u90fd\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u3002<\/p>\n<h2>map\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9<\/h2>\n<p>groovy\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9\u53ef\u4ee5\u91c7\u7528\u591a\u79cd\u65b9\u5f0f\uff0c\u64cd\u4f5c\u8d77\u6765\u5341\u5206\u65b9\u4fbf\uff0c\u5177\u4f53\u8bf7\u770b\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">def student = [:] \/\/ \u5b9a\u4e49\u4e00\u4e2a\u7a7a\u7684map\nstudent.put(&#039;name&#039;, &#039;John&#039;) \/\/ java\u8bed\u6cd5\u7684\u65b9\u5f0f\u6dfb\u52a0\u952e\u503c\u5bf9\nstudent[&#039;surname&#039;] = &#039;Doe&#039; \/\/ \u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e2d\u62ec\u53f7\u7684\u65b9\u5f0f\u6dfb\u52a0\u952e\u503c\u5bf9\nstudent &lt;&lt; [age: 17] \/\/ \u8fd9\u79cd\u65b9\u5f0f\u7c7b\u4f3c\u4e8eunix\u7684\u91cd\u5b9a\u5411\u7b26\u53f7\uff0c\u5c06\u952e\u503c\u5bf9\u653e\u5230map\u4e2d\nstudent.class = &quot;11C&quot; \/\/\u4f7f\u7528.\u70b9\u53f7\u7684\u65b9\u5f0f\u4e5f\u662f\u53ef\u4ee5\u7684\nstudent.&#039;school&#039; = &quot;Groovy School&quot; \/\/\u540c\u4e0a\u9762\u7c7b\u4f3c\uff0c\u4f46\u662f\u53ef\u4ee5\u653e\u4e00\u4e9b\u7279\u6b8a\u7b26\u53f7\n\nprintln student \/\/ [name:John, surname:Doe, age:17, class:11C, school:Groovy School]<\/code><\/pre>\n<h2>map\u4e2d\u5220\u9664\u952e\u503c\u5bf9<\/h2>\n<p>\u4ecemap\u4e2d\u5220\u9664\u952e\u503c\u5bf9\u50cf\u6dfb\u52a0\u4e00\u6837\u7b80\u5355\uff0c\u4f60\u53ef\u4ee5\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5220\u9664\u3002<\/p>\n<pre><code class=\"language-java\">def student = [name: &#039;John&#039;, surname: &#039;Doe&#039;, age: 17, class: &#039;11C&#039;, school: &#039;Groovy School&#039;]\nprintln student \/\/ [name:John, surname:Doe, age:17, class:11C, school:Groovy School]\n\n\/\/\u4f7f\u7528remove\u65b9\u6cd5\u5220\u9664\nstudent.remove(&#039;age&#039;) \/\/ Remove by key\nprintln student \/\/ [name:John, surname:Doe, class:11C, school:Groovy School]\n\n\/\/\u4f7f\u7528\u51cf\u6cd5\u5220\u9664\uff0c\u4e0d\u4f1a\u6539\u53d8\u539fmap\nstudent = student - [school: &#039;Groovy School&#039;] \/\/ This is something like arithmetic operation\nprintln student \/\/ [name:John, surname:Doe, class:11C]<\/code><\/pre>\n<h2>map\u83b7\u53d6\u503c\u4e3anull\u65f6\u8fd4\u56de\u9ed8\u8ba4\u503c<\/h2>\n<p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u4e8emap\u4e2d\u7684\u8fd4\u56de\u503c\u8c03\u7528\u51fd\u6570\uff0c\u5982\u679c\u8fd4\u56de\u503c\u4e3anull\u7684\u65f6\u5019\u4f1a\u62a5<code>NullPointerException<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u51fa\u73b0\u8fd9\u4e2a\u5f02\u5e38\uff0c\u9700\u8981\u68c0\u67e5\u662f\u5426\u4e3anull\u3002groovy\u4e3a\u4e86\u907f\u514d\u6211\u4eec\u83b7\u53d6\u5230null\uff0c\u63d0\u4f9b\u4e86\u65b9\u6cd5\u5f53\u8fd4\u56de\u503c\u4e3anull\u65f6\uff0c\u81ea\u52a8\u8fd4\u56de\u9ed8\u8ba4\u503c\u3002\u8bf7\u53c2\u8003\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">def student = [name: &#039;John&#039;, surname: &#039;Doe&#039;, age: 17, class: &#039;11C&#039;, school: &#039;Groovy School&#039;]\n\n\/\/map\u4e2d\u4e0d\u5b58\u5728key\u4e3a&#039;teacher&#039;\u7684\u952e\u503c\u5bf9\uff0c\u6240\u4ee5\u8fd4\u56denew Teacher(name : &quot;Betty&quot;)\ndef result = student.get(&#039;teacher&#039;, new Teacher(name: &quot;Betty&quot;))\n\nprintln result \/\/ Teacher(name:Betty)<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cmap\u4e2d\u4e0d\u5b58\u5728&#8217;teacher&#8217;\u6240\u4ee5\u8fd4\u56de<code>new Teacher()<\/code>\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u6211\u4eec\u8003\u8651<code>NullPointerException<\/code>\u7684\u5904\u7406\u3002<\/p>\n<h2>map\u5408\u5e76<\/h2>\n<p>groovy\u4e2d\u7684map\u5408\u5e76\u4e5f\u662f\u975e\u5e38\u7b80\u5355\uff0c\u5c31\u50cf\u5b57\u7b26\u4e32\u8fde\u63a5\u90a3\u6837\u3002<\/p>\n<pre><code class=\"language-java\">def studentProp1 = [name: &#039;John&#039;, surname: &#039;Doe&#039;]\ndef studentProp2 = [age: 17, class: &#039;11C&#039;]\ndef studentProp3 = [school: &#039;Groovy School&#039;]\n\n\/\/\u4f7f\u7528\u52a0\u6cd5\u5408\u5e76\u591a\u4e2amap\ndef student = studentProp1 + studentProp2 + studentProp3\n\n\/\/\u5408\u5e76\u540e\u7684\u7ed3\u679c\nprintln student \/\/ [name:John, surname:Doe, age:17, class:11C, school:Groovy School]<\/code><\/pre>\n<p>map\u5bf9\u8c61\u5408\u5e76\u5728groovy\u4e2d\u53ef\u4ee5\u5f88\u5bb9\u6613\u5b9e\u73b0\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>1\uff09\u4f7f\u7528+\u53f7<\/p>\n<pre><code class=\"language-java\">assert [ a: true, b: false ] + [ a: false ] == [ a: false, b: false ]<\/code><\/pre>\n<p>2\uff09\u6216\u8005\u4f7f\u7528\u5de6\u79fb\u64cd\u4f5c\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">assert [ a: true, b: false ] &lt;&lt; [ a: false ] == [ a: false, b: false ]<\/code><\/pre>\n<p>\u4e24\u8005\u7684\u533a\u522b\u662f\uff1a<\/p>\n<ul>\n<li>\u5de6\u79fb\u64cd\u4f5c\u662f\u628a\u53f3\u8fb9\u7684map\u5408\u5e76\u5230\u5de6\u8fb9\u7684map\u4e2d\uff0c\u6539\u53d8\u4e86\u5de6\u8fb9map\u7684\u5185\u5bb9<\/li>\n<li>\u4f7f\u7528<code>+<\/code>\u5219\u6839\u636e\u5de6\u8fb9\u7684map\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684map\uff0c\u5e76\u5c06\u53f3\u8fb9\u7684map\u5408\u5e76\u5230\u65b0map\u4e2d<\/li>\n<\/ul>\n<h2>\u83b7\u53d6\u4e24\u4e2amap\u7684\u4ea4\u96c6<\/h2>\n<p>\u6709\u65f6\u5019\u5f53\u4f60\u9700\u8981\u5728\u4e0d\u540c\u7684map\u4e2d\u67e5\u627e\u76f8\u540c\u952e\u503c\u5bf9\u7684\u65f6\u5019\uff0cgroovy\u4e3a\u4e86\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u83b7\u53d6map\u4ea4\u96c6\u3002<\/p>\n<pre><code class=\"language-java\">def student1 = [name: &#039;John&#039;, surname: &#039;Doe&#039;, age: 17]\ndef student2 = [surname: &#039;Bunny&#039;, age: 17]\n\n\/\/\u83b7\u53d6student1\u548cstudent2\u7684\u4ea4\u96c6\ndef sameProperties = student1.intersect(student2) \/\/ [age: 17]\n\nprintln sameProperties \/\/ [age:17]<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u627e\u51fastudent1\u548cstudent2\u4e24\u4e2amap\u7684\u952e\u503c\u5bf9\u4ea4\u96c6\u3002\u5bb9\u6613\u53d1\u73b0<code>age:17<\/code>\u662f\u4e24\u4e2amap\u90fd\u6709\u7684\u952e\u503c\u5bf9\uff0c\u4ea4\u96c6\u4e3a<code>[age:17]<\/code><\/p>\n<h2>\u83b7\u53d6map\u7684\u952e(key)\u96c6\u5408\u4e0e\u503c(value)\u96c6\u5408<\/h2>\n<p>groovy\u4e2d\u53ef\u4ee5\u50cfjava\u4e2d\u90a3\u6837\u83b7\u53d6map\u7684\u952e\u3001\u503c\u7684\u96c6\u5408\u3002<\/p>\n<pre><code class=\"language-java\">def student = [name: &#039;John&#039;, surname: &#039;Doe&#039;, age: 17]\n\n\/\/\u83b7\u53d6\u952ekey\u96c6\u5408\ndef studentKeys = student.keySet() \/\/ [name, surname, age]\nprintln studentKeys \/\/ [name, surname, age]\n\n\/\/\u83b7\u53d6\u503cvalue\u96c6\u5408\ndef studentValues = student.values() \/\/ [John, Doe, age]\nprintln studentValues \/\/ [John, Doe, 17]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>map\u7684\u5b9a\u4e49 map\u4e00\u822c\u7528\u4e8ekey-value\u952e\u503c\u5bf9\u7684\u5b58\u653e\u3002\u5728groovy\u4e2d\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u65b9\u5f0f\u5b9a\u4e49map\uff1a 1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[568],"tags":[],"class_list":["post-2233","post","type-post","status-publish","format-standard","hentry","category-groovy"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2233"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2233\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}