{"id":65863,"date":"2025-05-03T13:07:20","date_gmt":"2025-05-03T05:07:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65863\/"},"modified":"2025-05-03T13:07:20","modified_gmt":"2025-05-03T05:07:20","slug":"java%e9%87%8c%e6%80%8e%e4%b9%88%e6%8a%8a%e6%95%b0%e7%bb%84%e8%bd%ac%e6%88%90map-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65863\/","title":{"rendered":"Java\u91cc\u600e\u4e48\u628a\u6570\u7ec4\u8f6c\u6210map"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7 arrays.stream() \u548c collectors.tomap() \u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a map\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a\u521b\u5efa\u6570\u7ec4\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6d41\u3002\u4f7f\u7528 collectors.tomap() \u6307\u5b9a\u952e\u548c\u503c\u6620\u5c04\u51fd\u6570\u3002\u8fd4\u56de\u8f6c\u6362\u540e\u7684 map\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111313183686760.jpg\" class=\"aligncenter\" title=\"Java\u91cc\u600e\u4e48\u628a\u6570\u7ec4\u8f6c\u6210map\u63d2\u56fe\" alt=\"Java\u91cc\u600e\u4e48\u628a\u6570\u7ec4\u8f6c\u6210map\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u6570\u7ec4\u8f6c\u6362\u4e3a Map<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 Arrays.stream() \u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d41\uff0c\u518d\u4f7f\u7528 Collectors.toMap() \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a Map\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u521b\u5efa\u6570\u7ec4\uff1a<\/strong>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u539f\u59cb\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u8981\u8f6c\u6362\u4e3a Map \u7684\u6570\u636e\u3002<\/li>\n<li> <strong>\u521b\u5efa\u6d41\uff1a<\/strong>\u4f7f\u7528 Arrays.stream(array) \u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d41\u3002<\/li>\n<li>\n<p><strong>\u4f7f\u7528 Collectors.toMap()\uff1a<\/strong>\u4f7f\u7528 Collectors.toMap(Function keyMapper, Function valueMapper) \u65b9\u6cd5\u5c06\u6d41\u8f6c\u6362\u4e3a Map\u3002\u5176\u4e2d\uff1a <\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>keyMapper \u662f\u5c06\u6d41\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a Map \u952e\u7684\u51fd\u6570\u3002<\/li>\n<li>valueMapper \u662f\u5c06\u6d41\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a Map \u503c\u7684\u51fd\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<li> <strong>\u8fd4\u56de Map\uff1a<\/strong>Collectors.toMap() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u8f6c\u6362\u540e\u7684 Map\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>int[] array = {1, 2, 3, 4, 5};\n\nMap&lt;Integer, Integer&gt; map = Arrays.stream(array)\n        .boxed() \/\/ \u5c06\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\n        .collect(Collectors.toMap(i -&gt; i, i -&gt; i));\n\nSystem.out.println(map); \/\/ \u8f93\u51fa\uff1a{1=1, 2=2, 3=3, 4=4, 5=5}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u4ee5\u4e0a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u4f7f\u7528 Collectors.toMap() \u65b9\u6cd5\u3002<\/li>\n<li>Collectors.toMap() \u65b9\u6cd5\u9700\u8981\u6307\u5b9a\u952e\u548c\u503c\u7684\u6620\u5c04\u51fd\u6570\u3002\u5728\u793a\u4f8b\u4e2d\uff0c\u952e\u548c\u503c\u90fd\u76f8\u540c\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528 Function.identity() \u51fd\u6570\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fJava\u91cc\u600e\u4e48\u628a\u6570\u7ec4\u8f6c\u6210map\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7 arrays.stream() \u548c collectors.tomap() \u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a map\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a\u521b\u5efa\u6570\u7ec4\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6d41\u3002\u4f7f\u7528 collectors.tomap() \u6307\u5b9a\u952e\u548c\u503c\u6620\u5c04\u51fd\u6570\u3002\u8fd4\u56de\u8f6c\u6362\u540e\u7684 map\u3002 \u5982\u4f55\u5c06 Java \u6570\u7ec4\u8f6c\u6362\u4e3a Map \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 Arrays.stream() \u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d41\uff0c\u518d\u4f7f\u7528 Collectors.toMap() \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a Map\u3002 \u8be6\u7ec6\u6b65\u9aa4\uff1a \u521b\u5efa\u6570\u7ec4\uff1a\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u539f\u59cb\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u8981\u8f6c\u6362\u4e3a Map \u7684\u6570\u636e\u3002 \u521b\u5efa\u6d41\uff1a\u4f7f\u7528 Arrays.stream(array) \u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d41\u3002 \u4f7f\u7528 Collectors.toMap()\uff1a\u4f7f\u7528 Collectors.toMap(Function keyMapper, Function valueMapper) \u65b9\u6cd5\u5c06\u6d41\u8f6c\u6362\u4e3a Map\u3002\u5176\u4e2d\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b keyMapper \u662f\u5c06\u6d41\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a Map \u952e\u7684\u51fd\u6570\u3002 valueMapper \u662f\u5c06\u6d41\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a Map \u503c\u7684\u51fd\u6570\u3002 \u8fd4\u56de Map\uff1aCollectors.toMap() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u8f6c\u6362\u540e\u7684 Map\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a int[] array = {1, 2, [&hellip;]<\/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-65863","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65863","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=65863"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65863\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}