{"id":36483,"date":"2024-11-26T08:01:27","date_gmt":"2024-11-26T00:01:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36483\/"},"modified":"2024-11-26T08:01:27","modified_gmt":"2024-11-26T00:01:27","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-map-%e5%ad%98%e5%82%a8%e4%b8%8d%e5%90%8c%e6%96%b9%e6%b3%95%e5%bc%95%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36483\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Map \u5b58\u50a8\u4e0d\u540c\u65b9\u6cd5\u5f15\u7528\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173121482285690.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Map \u5b58\u50a8\u4e0d\u540c\u65b9\u6cd5\u5f15\u7528\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Map \u5b58\u50a8\u4e0d\u540c\u65b9\u6cd5\u5f15\u7528\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7528\u6620\u5c04\uff08map\uff09\u50a8\u5b58\u4e0d\u540c\u65b9\u6cd5\u5f15\u7528\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5c06\u4e0d\u540c\u7c7b\u7684 getter \u65b9\u6cd5\u7ec4\u7ec7\u8d77\u6765\uff0c\u4ee5\u65b9\u4fbf\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5177\u6709\u4e0d\u540c\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u56e0\u6b64\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528\u5177\u6709\u76f8\u540c\u6cdb\u578b\u7c7b\u578b\u53c2\u6570\u7684\u65b9\u6cd5\u5f15\u7528\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6cdb\u578b\u6765\u521b\u5efa\u6620\u5c04\uff08map\uff09\uff1a<\/p>\n<pre>map&lt;string, function&lt;?, ?&gt;&gt; map = new hashmap&lt;&gt;();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u65b9\u6cd5\u5f15\u7528\uff0c\u6211\u4eec\u9700\u8981\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u901a\u7528\u7c7b\u578b\u7684 function \u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u8981\u5b58\u50a8 group::getid\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre>map.put(\"id\", (function&lt;group, long&gt;) group::getid);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5f53\u6211\u4eec\u60f3\u8981\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5f15\u7528\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u4ece\u6620\u5c04\u4e2d\u83b7\u53d6\u5b83\u4eec\u3002\u4f8b\u5982\uff0c\u4ee5\u83b7\u53d6 group \u7684 id \u4e3a\u4f8b\uff1a<\/p>\n<pre>long id = map.get(\"id\").apply(group);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 Map \u5b58\u50a8\u4e0d\u540c\u65b9\u6cd5\u5f15\u7528\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528\u6620\u5c04\uff08map\uff09\u50a8\u5b58\u4e0d\u540c\u65b9\u6cd5\u5f15\u7528\u7684\u65b9\u6cd5 \u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5c06\u4e0d\u540c\u7c7b\u7684 getter \u65b9\u6cd5\u7ec4\u7ec7\u8d77\u6765\uff0c\u4ee5\u65b9\u4fbf\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5177\u6709\u4e0d\u540c\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u56e0\u6b64\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528\u5177\u6709\u76f8\u540c\u6cdb\u578b\u7c7b\u578b\u53c2\u6570\u7684\u65b9\u6cd5\u5f15\u7528\u3002 \u89e3\u51b3\u65b9\u6848 \u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6cdb\u578b\u6765\u521b\u5efa\u6620\u5c04\uff08map\uff09\uff1a map&lt;string, function&lt;?, ?&gt;&gt; map = new hashmap&lt;&gt;(); \u767b\u5f55\u540e\u590d\u5236 \u5bf9\u4e8e\u6bcf\u4e2a\u65b9\u6cd5\u5f15\u7528\uff0c\u6211\u4eec\u9700\u8981\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u901a\u7528\u7c7b\u578b\u7684 function \u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u8981\u5b58\u50a8 group::getid\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a map.put(&#8220;id&#8221;, (function&lt;group, long&gt;) group::getid); \u767b\u5f55\u540e\u590d\u5236 \u5f53\u6211\u4eec\u60f3\u8981\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5f15\u7528\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u4ece\u6620\u5c04\u4e2d\u83b7\u53d6\u5b83\u4eec\u3002\u4f8b\u5982\uff0c\u4ee5\u83b7\u53d6 group \u7684 id \u4e3a\u4f8b\uff1a long id = map.get(&#8220;id&#8221;).apply(group); \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 Map \u5b58\u50a8\u4e0d\u540c\u65b9\u6cd5\u5f15\u7528\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-36483","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=36483"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36483\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}