{"id":36457,"date":"2024-11-26T13:06:41","date_gmt":"2024-11-26T05:06:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36457\/"},"modified":"2024-11-26T13:06:41","modified_gmt":"2024-11-26T05:06:41","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-java-%e4%b8%ad%e5%a3%b0%e6%98%8e%e5%ae%b9%e7%ba%b3%e4%b8%8d%e5%90%8c%e7%b1%bb%e5%9e%8b%e6%96%b9%e6%b3%95%e5%bc%95%e7%94%a8%e7%9a%84-map%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36457\/","title":{"rendered":"\u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u5bb9\u7eb3\u4e0d\u540c\u7c7b\u578b\u65b9\u6cd5\u5f15\u7528\u7684 Map\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173115993524134.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u5bb9\u7eb3\u4e0d\u540c\u7c7b\u578b\u65b9\u6cd5\u5f15\u7528\u7684 Map\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u5bb9\u7eb3\u4e0d\u540c\u7c7b\u578b\u65b9\u6cd5\u5f15\u7528\u7684 Map\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u58f0\u660e\u5bb9\u7eb3\u65b9\u6cd5\u5f15\u7528\u7684\u6620\u5c04\uff08map\uff09<\/strong><\/p>\n<p>\u4e3a\u4e86\u5c06\u4e0d\u540c\u7c7b\u7684\u591a\u4e2a\u65b9\u6cd5\u5f15\u7528\u5b58\u50a8\u5728\u4e00\u4e2a\u6620\u5c04\u4e2d\uff0c\u9700\u8981\u58f0\u660e\u5bb9\u7eb3\u65b9\u6cd5\u5f15\u7528\u7684\u51fd\u6570\u5f0f\u63a5\u53e3\u3002\u7136\u800c\uff0c\u7531\u4e8e\u65b9\u6cd5\u5f15\u7528\u7684\u6cdb\u578b\u53c2\u6570\u4e0d\u540c\uff0c\u58f0\u660e\u6620\u5c04\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u56f0\u96be\u3002<\/p>\n<p>\u6839\u636e\u95ee\u9898\u56de\u7b54\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 ide \u7684\u4ee3\u7801\u63d0\u793a\u529f\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u65b9\u6cd5\u5f15\u7528\u7684\u5217\u8868\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>list&lt;function&lt;object, ?&gt;&gt; methods = arrays.aslist(user::getusername, post::getid);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\uff0c\u4f7f\u7528 ide \u7684\u63d0\u793a\u529f\u80fd\uff08\u4f8b\u5982\u5728  \u4e2d\u8f93\u5165 &#8220;.var&#8221;)\uff0c\u786e\u5b9a\u5217\u8868\u7684\u7c7b\u578b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\nlist&lt;? extends function&lt;?, ?&gt;&gt; methods;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b64\u5217\u8868\u7c7b\u578b\u58f0\u660e\u6620\u5c04\uff1a<\/p>\n<pre>\nmap&lt;string, function&lt;?, ?&gt;&gt; map = new hashmap&lt;&gt;();\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8981\u5c06\u65b9\u6cd5\u5f15\u7528\u653e\u5165\u6620\u5c04\u4e2d\uff0c\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u8981\u653e\u5165 group::getid\uff0c\u53ef\u4ee5\u8fd9\u6837\u7f16\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>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u5bb9\u7eb3\u4e0d\u540c\u7c7b\u578b\u65b9\u6cd5\u5f15\u7528\u7684 Map\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>\u58f0\u660e\u5bb9\u7eb3\u65b9\u6cd5\u5f15\u7528\u7684\u6620\u5c04\uff08map\uff09 \u4e3a\u4e86\u5c06\u4e0d\u540c\u7c7b\u7684\u591a\u4e2a\u65b9\u6cd5\u5f15\u7528\u5b58\u50a8\u5728\u4e00\u4e2a\u6620\u5c04\u4e2d\uff0c\u9700\u8981\u58f0\u660e\u5bb9\u7eb3\u65b9\u6cd5\u5f15\u7528\u7684\u51fd\u6570\u5f0f\u63a5\u53e3\u3002\u7136\u800c\uff0c\u7531\u4e8e\u65b9\u6cd5\u5f15\u7528\u7684\u6cdb\u578b\u53c2\u6570\u4e0d\u540c\uff0c\u58f0\u660e\u6620\u5c04\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u56f0\u96be\u3002 \u6839\u636e\u95ee\u9898\u56de\u7b54\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 ide \u7684\u4ee3\u7801\u63d0\u793a\u529f\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u65b9\u6cd5\u5f15\u7528\u7684\u5217\u8868\uff0c\u4f8b\u5982\uff1a list&lt;function&lt;object, ?&gt;&gt; methods = arrays.aslist(user::getusername, post::getid); \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\uff0c\u4f7f\u7528 ide \u7684\u63d0\u793a\u529f\u80fd\uff08\u4f8b\u5982\u5728 \u4e2d\u8f93\u5165 &#8220;.var&#8221;)\uff0c\u786e\u5b9a\u5217\u8868\u7684\u7c7b\u578b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b list&lt;? extends function&lt;?, ?&gt;&gt; methods; \u767b\u5f55\u540e\u590d\u5236 \u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b64\u5217\u8868\u7c7b\u578b\u58f0\u660e\u6620\u5c04\uff1a map&lt;string, function&lt;?, ?&gt;&gt; map = new hashmap&lt;&gt;(); \u767b\u5f55\u540e\u590d\u5236 \u8981\u5c06\u65b9\u6cd5\u5f15\u7528\u653e\u5165\u6620\u5c04\u4e2d\uff0c\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u8981\u653e\u5165 group::getid\uff0c\u53ef\u4ee5\u8fd9\u6837\u7f16\u5199\uff1a map.put(&#8220;id&#8221;, (Function&lt;Group, Long&gt;) Group::getId); \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u5bb9\u7eb3\u4e0d\u540c\u7c7b\u578b\u65b9\u6cd5\u5f15\u7528\u7684 Map\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-36457","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36457","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=36457"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36457\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}