{"id":39125,"date":"2024-11-26T16:32:32","date_gmt":"2024-11-26T08:32:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39125\/"},"modified":"2024-11-26T16:32:32","modified_gmt":"2024-11-26T08:32:32","slug":"java%e9%87%8c%e9%9d%a2map%e6%80%8e%e4%b9%88%e5%ad%98%e6%94%be%e5%88%b0%e6%95%b0%e7%bb%84%e9%87%8c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39125\/","title":{"rendered":"java\u91cc\u9762map\u600e\u4e48\u5b58\u653e\u5230\u6570\u7ec4\u91cc"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5c06 map \u5b58\u50a8\u5230\u6570\u7ec4\u4e2d\uff1a\u4f7f\u7528 map.entryset() \u65b9\u6cd5\u83b7\u53d6 map \u4e2d\u7684\u6761\u76ee\u96c6\uff0c\u5373\u952e\u503c\u5bf9\u7684\u96c6\u5408\u3002\u4f7f\u7528\u6570\u7ec4\u6784\u9020\u51fd\u6570\u521b\u5efa\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u6761\u76ee\u96c6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111604195560692.jpg\" class=\"aligncenter\" title=\"java\u91cc\u9762map\u600e\u4e48\u5b58\u653e\u5230\u6570\u7ec4\u91cc\u63d2\u56fe\" alt=\"java\u91cc\u9762map\u600e\u4e48\u5b58\u653e\u5230\u6570\u7ec4\u91cc\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Map \u5b58\u653e\u5230 Java \u6570\u7ec4\u4e2d<\/strong><\/p>\n<p>Java \u4e2d\u7684 Map \u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u3002\u6570\u7ec4\u662f\u4e00\u79cd\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u5143\u7d20\u7684\u56fa\u5b9a\u5927\u5c0f\u96c6\u5408\u3002\u4e3a\u4e86\u5c06 Map \u5b58\u653e\u5230\u6570\u7ec4\u4e2d\uff0c\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 Map.entrySet() \u65b9\u6cd5\u83b7\u53d6 Map \u4e2d\u7684\u6761\u76ee\u96c6\uff08\u5373\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff09\u3002\u7136\u540e\uff0c\u4f7f\u7528\u6570\u7ec4\u6784\u9020\u51fd\u6570\u521b\u5efa\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u6761\u76ee\u96c6\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();\n\/\/ \u6dfb\u52a0\u952e\u503c\u5bf9\u5230 map \u4e2d\nmap.put(\"apple\", 1);\nmap.put(\"banana\", 2);\n\n\/\/ \u83b7\u53d6 map \u4e2d\u7684\u6761\u76ee\u96c6\nSet&lt;Map.Entry&lt;String, Integer&gt;&gt; entrySet = map.entrySet();\n\n\/\/ \u521b\u5efa\u5305\u542b\u6761\u76ee\u96c6\u7684\u6570\u7ec4\nMap.Entry&lt;String, Integer&gt;[] entriesArray = entrySet.toArray(new Map.Entry[0]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>map.entrySet() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a Set \u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b Map \u4e2d\u7684\u6240\u6709\u6761\u76ee\u3002<\/li>\n<li>new Map.Entry[0] \u521b\u5efa\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff0c\u5176\u5143\u7d20\u7c7b\u578b\u4e3a Map.Entry \u3002 <\/li>\n<li>entrySet.toArray() \u5c06\u6761\u76ee\u96c6\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728 entriesArray \u4e2d\u3002<\/li>\n<\/ul>\n<p>\u73b0\u5728\uff0centriesArray \u6570\u7ec4\u5305\u542b\u4e86 map \u4e2d\u7684\u952e\u503c\u5bf9\u3002\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u8bbf\u95ee\u6bcf\u4e2a\u6761\u76ee\uff0c\u4ece\u800c\u83b7\u53d6\u952e\u548c\u503c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u91cc\u9762map\u600e\u4e48\u5b58\u653e\u5230\u6570\u7ec4\u91cc\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>\u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5c06 map \u5b58\u50a8\u5230\u6570\u7ec4\u4e2d\uff1a\u4f7f\u7528 map.entryset() \u65b9\u6cd5\u83b7\u53d6 map \u4e2d\u7684\u6761\u76ee\u96c6\uff0c\u5373\u952e\u503c\u5bf9\u7684\u96c6\u5408\u3002\u4f7f\u7528\u6570\u7ec4\u6784\u9020\u51fd\u6570\u521b\u5efa\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u6761\u76ee\u96c6\u3002 \u5982\u4f55\u5c06 Map \u5b58\u653e\u5230 Java \u6570\u7ec4\u4e2d Java \u4e2d\u7684 Map \u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u3002\u6570\u7ec4\u662f\u4e00\u79cd\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u5143\u7d20\u7684\u56fa\u5b9a\u5927\u5c0f\u96c6\u5408\u3002\u4e3a\u4e86\u5c06 Map \u5b58\u653e\u5230\u6570\u7ec4\u4e2d\uff0c\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u8f6c\u6362\u3002 \u65b9\u6cd5\uff1a \u4f7f\u7528 Map.entrySet() \u65b9\u6cd5\u83b7\u53d6 Map \u4e2d\u7684\u6761\u76ee\u96c6\uff08\u5373\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff09\u3002\u7136\u540e\uff0c\u4f7f\u7528\u6570\u7ec4\u6784\u9020\u51fd\u6570\u521b\u5efa\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u6761\u76ee\u96c6\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4ee3\u7801\u793a\u4f8b\uff1a Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;(); \/\/ \u6dfb\u52a0\u952e\u503c\u5bf9\u5230 map \u4e2d map.put(&#8220;apple&#8221;, 1); map.put(&#8220;banana&#8221;, 2); \/\/ \u83b7\u53d6 map \u4e2d\u7684\u6761\u76ee\u96c6 Set&lt;Map.Entry&lt;String, Integer&gt;&gt; entrySet = map.entrySet(); \/\/ \u521b\u5efa\u5305\u542b\u6761\u76ee\u96c6\u7684\u6570\u7ec4 Map.Entry&lt;String, Integer&gt;[] [&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-39125","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39125","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=39125"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39125\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}