{"id":203711,"date":"2025-05-22T11:20:26","date_gmt":"2025-05-22T03:20:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203711\/"},"modified":"2025-05-22T11:20:26","modified_gmt":"2025-05-22T03:20:26","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e6%88%91%e7%9a%84-foreach-lambda-%e4%b8%8d%e5%85%81%e8%ae%b8%e6%88%91%e4%bd%bf%e7%94%a8-java-%e4%b8%ad%e7%9a%84-return-%e8%af%ad%e5%8f%a5%e9%80%80%e5%87%ba%e5%87%bd","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203711\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u6211\u7684 forEach lambda \u4e0d\u5141\u8bb8\u6211\u4f7f\u7528 Java \u4e2d\u7684 return \u8bed\u53e5\u9000\u51fa\u51fd\u6570\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4e3a\u4ec0\u4e48\u6211\u7684 forEach lambda \u4e0d\u5141\u8bb8\u6211\u4f7f\u7528 Java \u4e2d\u7684 return \u8bed\u53e5\u9000\u51fa\u51fd\u6570\uff1f<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u4e3a\u4ec0\u4e48\u6211\u7684 forEach lambda \u4e0d\u5141\u8bb8\u6211\u4f7f\u7528 Java \u4e2d\u7684 return \u8bed\u53e5\u9000\u51fa\u51fd\u6570\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1731978585673be559d9645.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5047\u8bbe\u4f60\u6709\u4e00\u7bee\u5b50\u98df\u7269\uff1a<\/p>\n<pre>list&lt;food&gt; basket = list.of(\n     new food(\"apple\", fruit),\n     new food(\"banana\", fruit),\n     new food(\"carrot\", vegetable),\n     new food(\"orange\", fruit),\n);\n<\/pre>\n<p>\u5e76\u4e14\u8981\u6c42\u4ec5\u63a5\u53d7\u4ec5\u88c5\u6ee1\u6c34\u679c\u7684\u7bee\u5b50\u3002\u4e3a\u4e86\u6ee1\u8db3\u8fd9\u4e2a\u8981\u6c42\uff0c\u60a8\u51b3\u5b9a\u5b9e\u73b0\u4e00\u4e2a for \u5faa\u73af\uff1a<\/p>\n<pre>private boolean containtsonlyfruits(list&lt;food&gt; basket) {\nfor (food food : basket) {\n        if (food.getfoodtype() != fruit) {\n            return false;\n        }\n    }\n    return true;\n}\n<\/pre>\n<p>\u7136\u540e\uff0c\u60a8\u8bb0\u5f97\u60a8\u4e00\u76f4\u5728\u5b66\u4e60 java lambda \u548c\u66f4\u5b9e\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u6b64\u60a8\u51b3\u5b9a\u5728 foreach lambda \u4e2d\u7f16\u5199\u76f8\u540c\u7684\u5185\u5bb9\uff1a<\/p>\n<pre>private boolean containtsonlyfruits(list&lt;food&gt; basket) {\n    basket.foreach(food -&gt; {\n        if (food.getfoodtype() != fruit) {\n            return false;\n        }\n    });\n    return true;\n}\n<\/pre>\n<p>\u53ea\u662f\u9047\u5230\u4e00\u4e2a\u9519\u8bef\uff1a<\/p>\n<p>\u610f\u5916\u7684\u8fd4\u56de\u503c<\/p>\n<p>\u8fd9\u662f\u4e3a\u4ec0\u4e48\uff1f<\/p>\n<p>lambda \u53ea\u4e0d\u8fc7\u662f\u4e00\u4e2a\u51fd\u6570\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u6216\u8005\u6362\u53e5\u8bdd\u8bf4\uff0c\u662f\u4e00\u4e2a\u6ca1\u6709\u540d\u79f0\u7684\u51fd\u6570\u3002\u5c31\u50cf\u4efb\u4f55\u5176\u4ed6\u51fd\u6570\u4e00\u6837\uff0clambda \u53ef\u4ee5\u63a5\u6536\u53c2\u6570\u5e76\u671f\u671b\u8fd4\u56de\u4e00\u4e9b\u5185\u5bb9\u3002<\/p>\n<p>\u5f53\u4f60\u5c1d\u8bd5\u4ece foreach lambda \u8fd4\u56de false \u65f6\uff0c\u4f60\u5b9e\u9645\u4e0a\u662f\u5728\u5c1d\u8bd5\u9000\u51fa\u8fd9\u4e2a\u533f\u540d\u51fd\u6570\u5e76\u8f93\u51fa\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u4f46\u662f\uff0cforeach \u51fd\u6570\u9884\u8ba1\u4e0d\u4f1a\u8fd4\u56de\u4efb\u4f55\u7ed3\u679c\u3002\u8fd9\u6839\u672c\u4e0d\u662f\u8fd9\u4e2a\u529f\u80fd\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u5982\u679c\u4f60\u770b\u4e00\u4e0b foreach \u51fd\u6570\u7684\u5b9e\u73b0\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u63a5\u53d7\u4e00\u4e2a consumer\uff1a<\/p>\n<pre>default void foreach(consumer&lt;? super t&gt; action) {\n    objects.requirenonnull(action);\n    for (t t : this) {\n        action.accept(t);\n    }\n}\n<\/pre>\n<p>\u6d88\u8d39\u8005\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u8868\u793a\u63a5\u53d7\u5355\u4e2a\u8f93\u5165\u53c2\u6570\u4e14\u4e0d\u8fd4\u56de\u7ed3\u679c\u7684\u64cd\u4f5c\u3002\u5728\u6b64\u5b9e\u73b0\u4e2d\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u5728\u5e95\u5c42\uff0cforeach \u51fd\u6570\u4f7f\u7528 foreach \u5faa\u73af\uff0c\u6267\u884c\u7ed9\u5b9a\u7684\u64cd\u4f5c\u5e76\u4e14\u4e0d\u8fd4\u56de\u4efb\u4f55\u5185\u5bb9\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u4e3a\u4e86\u6ee1\u8db3\u51fd\u6570\u5f0f\u65b9\u6cd5\u4e2d\u7684\u64cd\u4f5c\uff0c\u6211\u4eec\u9700\u8981\u627e\u5230\u53e6\u4e00\u4e2a lambda\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u5f88\u597d\u7684\u5019\u9009\u8005\uff1aallmatch\u3002<\/p>\n<p>\u4f7f\u7528 allmatch lambda\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u67e5\u7bee\u5b50\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u662f\u6c34\u679c\uff1a<\/p>\n<pre>private boolean onlyfruits(list&lt;food&gt; basket) {\n    return basket.stream().allmatch(food -&gt; food.getfoodtype() == fruit);\n}\n<\/pre>\n<p>\u5982\u679c\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u6211\u4eec\u7684 lambda \u5c06\u8fd4\u56de true\u3002\u5982\u679c\u6211\u4eec\u770b\u4e00\u4e0b allmatch \u7684\u5185\u90e8\uff1a<\/p>\n<pre>boolean allMatch(Predicate&lt;? super T&gt; predicate);\n<\/pre>\n<p>\u6211\u4eec\u5c06\u770b\u5230 allmatch \u51fd\u6570\u9700\u8981\u4e00\u4e2a\u8c13\u8bcd\u3002<\/p>\n<p>\u8c13\u8bcd\u662f\u4e00\u4e2a\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u5c31\u50cf\u6d88\u8d39\u8005\u4e00\u6837\uff0c\u4f46\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u6709\u70b9\u4e0d\u540c\u3002 consumer \u8868\u793a\u63a5\u53d7\u5355\u4e2a\u8f93\u5165\u53c2\u6570\u4e14\u4e0d\u8fd4\u56de\u7ed3\u679c\u7684\u64cd\u4f5c\uff0c\u800c predicate \u8868\u793a\u4e00\u4e2a\u53c2\u6570\u7684\u8c13\u8bcd\uff08\u5e03\u5c14\u503c\u51fd\u6570\uff09\uff0c\u7528\u4e8e\u6d4b\u8bd5\u5bf9\u8c61\u7684\u6761\u4ef6\u5e76\u8fd4\u56de\u5e03\u5c14\u503c\uff08true \u6216 false\uff09 ).<\/p>\n<p>\u8fd8\u6709\u5176\u4ed6\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\u63a5\u53e3\u3002\u4f60\u80fd\u544a\u8bc9\u6211\u5b83\u4eec\u662f\u4ec0\u4e48\u4ee5\u53ca\u5b83\u4eec\u5982\u4f55\u5de5\u4f5c\u5417\uff1f<\/p>\n<p>\u4fdd\u6301\u597d\u5947\u5fc3\uff01<\/p>\n<h2> \u8d21\u732e <\/h2>\n<p><strong><em>\u5199\u4f5c\u9700\u8981\u65f6\u95f4\u548c\u7cbe\u529b\u3002<\/em><\/strong>\u6211\u559c\u6b22\u5199\u4f5c\u548c\u5206\u4eab\u77e5\u8bc6\uff0c\u4f46\u6211\u4e5f\u6709\u8d26\u5355\u8981\u4ed8\u3002<\/p>\n<p>\u5982\u679c\u60a8\u559c\u6b22\u6211\u7684\u4f5c\u54c1\uff0c\u8bf7<strong>\u8003\u8651\u901a\u8fc7\u201c\u4e70\u676f\u5496\u5561\u201d\u6350\u6b3e\uff1ahttps:\/\/www.buymeacoffee.com\/raphaeldelio<\/strong><\/p>\n<p><strong><em>\u6216\u8005\u53d1\u9001\u7ed9\u6211\u6bd4\u7279\u5e01\uff1a1hjg7pmghg3z8rath4aiuwr156bgafj6zw<\/em><\/strong><\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u4e3a\u4ec0\u4e48\u6211\u7684 forEach lambda \u4e0d\u5141\u8bb8\u6211\u4f7f\u7528 Java \u4e2d\u7684 return \u8bed\u53e5\u9000\u51fa\u51fd\u6570\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48\u6211\u7684 forEach la&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203711","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203711"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203711\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}