{"id":65160,"date":"2025-05-03T10:19:53","date_gmt":"2025-05-03T02:19:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65160\/"},"modified":"2025-05-03T10:19:53","modified_gmt":"2025-05-03T02:19:53","slug":"%e6%b5%81%e4%b8%ad%e7%9a%84-foreach-%e5%92%8c-map-%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab%ef%bc%9f-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65160\/","title":{"rendered":"\u6d41\u4e2d\u7684 forEach \u548c map \u6709\u4ec0\u4e48\u533a\u522b\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173131436562947.jpg\" class=\"aligncenter\" title=\"\u6d41\u4e2d\u7684 forEach \u548c map \u6709\u4ec0\u4e48\u533a\u522b\uff1f\u63d2\u56fe\" alt=\"\u6d41\u4e2d\u7684 forEach \u548c map \u6709\u4ec0\u4e48\u533a\u522b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>foreach<\/strong>\uff1a\u5bf9\u6d41\u7684\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u64cd\u4f5c\u4f46\u4e0d\u8f6c\u6362\u6216\u8fd4\u56de\u6570\u636e\u7684\u7ec8\u7aef\u64cd\u4f5c\u3002<\/p>\n<p><strong>map<\/strong>\uff1a\u8f6c\u6362\u6d41\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u8fd4\u56de\u8f6c\u6362\u5143\u7d20\u7684\u65b0\u6d41\u3002<\/p>\n<p>\u5e26\u6709\u5217\u8868\u7684\u57fa\u672c foreach<\/p>\n<pre>import java.util.arrays;\nimport java.util.list;\n\npublic class foreachexample {\n    public static void main(string[] args) {\n        list&lt;string&gt; names = arrays.aslist(\"alice\", \"bob\", \"charlie\");\n\n        \/\/ print each name using foreach\n        names.foreach(name -&gt; system.out.println(name));\n    }\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<pre>List&lt;String&gt; names = Arrays.asList(\"Alice\", \"Bob\");\nnames.stream().forEach(System.out::println); \/\/ Simply prints each name\n\nList&lt;Integer&gt; nameLengths = names.stream()\n                                 .map(String::length) \/\/ Transforms each name to its length\n                                 .collect(Collectors.toList());\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6d41\u4e2d\u7684 forEach \u548c map \u6709\u4ec0\u4e48\uff1f\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>foreach\uff1a\u5bf9\u6d41\u7684\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u64cd\u4f5c\u4f46\u4e0d\u8f6c\u6362\u6216\u8fd4\u56de\u6570\u636e\u7684\u7ec8\u7aef\u64cd\u4f5c\u3002 map\uff1a\u8f6c\u6362\u6d41\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u8fd4\u56de\u8f6c\u6362\u5143\u7d20\u7684\u65b0\u6d41\u3002 \u5e26\u6709\u5217\u8868\u7684\u57fa\u672c foreach import java.util.arrays; import java.util.list; public class foreachexample { public static void main(string[] args) { list&lt;string&gt; names = arrays.aslist(&#8220;alice&#8221;, &#8220;bob&#8221;, &#8220;charlie&#8221;); \/\/ print each name using foreach names.foreach(name -&gt; system.out.println(name)); } } \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b List&lt;String&gt; names = Arrays.asList(&#8220;Alice&#8221;, &#8220;Bob&#8221;); names.stream().forEach(System.out::println); \/\/ Simply prints each name List&lt;Integer&gt; nameLengths = names.stream() .map(String::length) \/\/ Transforms [&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-65160","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65160","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=65160"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65160\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}