{"id":580,"date":"2022-07-16T10:35:42","date_gmt":"2022-07-16T02:35:42","guid":{"rendered":"http:\/\/blog.nonot.cn\/?p=580"},"modified":"2024-07-08T12:47:25","modified_gmt":"2024-07-08T04:47:25","slug":"map-entry%e5%ad%a6%e4%b9%a0%e5%92%8c%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2022\/07\/16\/map-entry%e5%ad%a6%e4%b9%a0%e5%92%8c%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Map.Entry\u5b66\u4e60\u548c\u8be6\u89e3"},"content":{"rendered":"<p>Map.Entry\u7684\u5b9a\u4e49<br \/>\nMap\u7684entrySet()\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5b9e\u73b0Map.Entry\u63a5\u53e3\u7684\u5bf9\u8c61\u96c6\u5408\u3002\u96c6\u5408\u4e2d\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u662f\u5e95\u5c42Map\u4e2d\u4e00\u4e2a\u7279\u5b9a\u7684\u952e\/\u503c\u5bf9\u3002\u901a\u8fc7\u8fd9\u4e2a\u96c6\u5408\u7684\u8fed\u4ee3\u5668\uff0c\u83b7\u5f97\u6bcf\u4e00\u4e2a\u6761\u76ee(\u552f\u4e00\u83b7\u53d6\u65b9\u5f0f)\u7684\u952e\u6216\u503c\u5e76\u5bf9\u503c\u8fdb\u884c\u66f4\u6539\u3002Map.Entry\u4e2d\u7684\u5e38\u7528\u65b9\u6cd5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>(1) Object getKey(): \u8fd4\u56de\u6761\u76ee\u7684\u5173\u952e\u5b57<br \/>\n(2) Object getValue(): \u8fd4\u56de\u6761\u76ee\u7684\u503c<br \/>\n(3) Object setValue(Object value): \u5c06\u76f8\u5173\u6620\u50cf\u4e2d\u7684\u503c\u6539\u4e3avalue\uff0c\u5e76\u4e14\u8fd4\u56de\u65e7\u503c<\/p>\n<p>Map.Entry\u7684\u4f5c\u7528<br \/>\nMap.Entry\u662f\u4e3a\u4e86\u66f4\u65b9\u4fbf\u7684\u8f93\u51famap\u952e\u503c\u5bf9\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u8981\u8f93\u51faMap\u4e2d\u7684key \u548c value \u662f\u5148\u5f97\u5230key\u7684\u96c6\u5408keySet()\uff0c\u7136\u540e\u518d\u8fed\u4ee3\uff08\u5faa\u73af\uff09\u7531\u6bcf\u4e2akey\u5f97\u5230\u6bcf\u4e2avalue\u3002values()\u65b9\u6cd5\u662f\u83b7\u53d6\u96c6\u5408\u4e2d\u7684\u6240\u6709\u503c\uff0c\u4e0d\u5305\u542b\u952e\uff0c\u6ca1\u6709\u5bf9\u5e94\u5173\u7cfb\u3002\u800cEntry\u53ef\u4ee5\u4e00\u6b21\u6027\u83b7\u5f97\u8fd9\u4e24\u4e2a\u503c\u3002<\/p>\n<p>\u5e38\u7528\u7684\u904d\u5386Map\u7684\u65b9\u6cd5<\/p>\n<p>Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();<br \/>\nmap.put(&#8220;1&#8221;, &#8220;value1&#8221;);<br \/>\nmap.put(&#8220;2&#8221;, &#8220;value2&#8221;);<br \/>\nmap.put(&#8220;3&#8221;, &#8220;value3&#8221;);<\/p>\n<p>\/\/\u7b2c\u4e00\u79cd\uff1a\u666e\u904d\u4f7f\u7528\uff0c\u7531\u4e8e\u4e8c\u6b21\u53d6\u503c,\u6548\u7387\u4f1a\u6bd4\u7b2c\u4e8c\u79cd\u548c\u7b2c\u4e09\u79cd\u6162\u4e00\u500d<br \/>\nSystem.out.println(&#8220;\u901a\u8fc7Map.keySet\u904d\u5386key\u548cvalue\uff1a&#8221;);<br \/>\nfor (String key : map.keySet()) {<br \/>\nSystem.out.println(&#8220;key= &#8220;+ key + &#8221; and value= &#8221; + map.get(key));<br \/>\n}<\/p>\n<p>\/\/\u7b2c\u4e8c\u79cd<br \/>\nSystem.out.println(&#8220;\u901a\u8fc7Map.entrySet\u4f7f\u7528iterator\u904d\u5386key\u548cvalue\uff1a&#8221;);<br \/>\nIterator&lt;Map.Entry&lt;String, String&gt;&gt; it = map.entrySet().iterator();<br \/>\nwhile (it.hasNext()) {<br \/>\nMap.Entry&lt;String, String&gt; entry = it.next();<br \/>\nSystem.out.println(&#8220;key= &#8221; + entry.getKey() + &#8221; and value= &#8221; + entry.getValue());<br \/>\n}<\/p>\n<p>\/\/\u7b2c\u4e09\u79cd\uff1a\u65e0\u6cd5\u5728for\u5faa\u73af\u65f6\u5b9e\u73b0remove\u7b49\u64cd\u4f5c<br \/>\nSystem.out.println(&#8220;\u901a\u8fc7Map.entrySet\u904d\u5386key\u548cvalue&#8221;);<br \/>\nfor (Map.Entry&lt;String, String&gt; entry : map.entrySet()) {<br \/>\nSystem.out.println(&#8220;key= &#8221; + entry.getKey() + &#8221; and value= &#8221; + entry.getValue());<br \/>\n}<\/p>\n<p>\/\/\u7b2c\u56db\u79cd\uff1a\u53ea\u80fd\u83b7\u53d6values,\u4e0d\u80fd\u83b7\u53d6key<br \/>\nSystem.out.println(&#8220;\u901a\u8fc7Map.values()\u904d\u5386\u6240\u6709\u7684value\uff0c\u4f46\u4e0d\u80fd\u904d\u5386key&#8221;);<br \/>\nfor (String v : map.values()) {<br \/>\nSystem.out.println(&#8220;value= &#8221; + v);<br \/>\n}<\/p>\n<p>Map.entrySet\u8fed\u4ee3\u5668\u4f1a\u751f\u6210EntryIterator,\u5176\u8fd4\u56de\u7684\u5b9e\u4f8b\u662f\u4e00\u4e2a\u5305\u542bkey\/value\u952e\u503c\u5bf9\u7684\u5bf9\u8c61\u3002\u800ckeySet\u4e2d\u8fed\u4ee3\u5668\u8fd4\u56de\u7684\u53ea\u662fkey\u5bf9\u8c61\uff0c\u8fd8\u9700\u8981\u5230map\u4e2d\u4e8c\u6b21\u53d6\u503c\u3002\u6545entrySet\u8981\u6bd4keySet\u5feb\u4e00\u500d\u5de6\u53f3\u3002<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Map.Entry\u7684\u5b9a\u4e49 Map\u7684entrySet()\u65b9\u6cd5\u8fd4&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-580","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/comments?post=580"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/580\/revisions"}],"predecessor-version":[{"id":1187,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/580\/revisions\/1187"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}