{"id":203018,"date":"2025-05-22T10:52:41","date_gmt":"2025-05-22T02:52:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203018\/"},"modified":"2025-05-22T10:52:41","modified_gmt":"2025-05-22T02:52:41","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e6%8e%a5%e5%8f%a3%e5%9c%a8%e6%b5%81%e5%a4%84%e7%90%86%e4%b8%ad%e7%9a%84%e8%bf%90%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203018\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6d41\u5904\u7406\u4e2d\u7684\u8fd0\u7528"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6d41\u5904\u7406\u4e2d\u7684\u8fd0\u7528<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6d41\u5904\u7406\u4e2d\u7684\u8fd0\u7528\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p>Java \u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6d41\u5904\u7406\u4e2d\u626e\u6f14\u7740\u81f3\u5173\u91cd\u8981\u7684\u89d2\u8272\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5b9a\u4e49\u548c\u4f20\u9012\u64cd\u4f5c\uff0c\u4ee5\u5904\u7406\u65e0\u9650\u91cf\u6570\u636e\u800c\u65e0\u9700\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002Java 8 \u4e2d\u5f15\u5165\u4e86 Stream API\uff0c\u5305\u542b\u7528\u4e8e\u5b9a\u4e49\u548c\u6267\u884c\u6d41\u5904\u7406\u64cd\u4f5c\u7684\u51fd\u6570\u5f0f\u63a5\u53e3\uff1aPredicate&lt;T&gt;\uff1a\u8fc7\u6ee4\u6d41\u4e2d\u7684\u5143\u7d20\u3002Function&lt;T, R&gt;\uff1a\u5c06\u5143\u7d20\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002Consumer&lt;T&gt;\uff1a\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u64cd\u4f5c\u3002Supplier&lt;T&gt;\uff1a\u521b\u5efa\u6d41\u7684\u5143\u7d20\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730021385671e08090d37f.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6d41\u5904\u7406\u4e2d\u7684\u8fd0\u7528<\/strong><\/p>\n<p>\u6d41\u5904\u7406\u662f\u4e00\u79cd\u5728\u6570\u636e\u6d41\u4e0a\u8fdb\u884c\u64cd\u4f5c\u7684\u7f16\u7a0b\u8303\u4f8b\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u5904\u7406\u65e0\u9650\u91cf\u7684\u6570\u636e\uff0c\u800c\u65e0\u9700\u5c06\u5b83\u4eec\u5168\u90e8\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6d41\u5904\u7406\u4e2d\u626e\u6f14\u7740\u81f3\u5173\u91cd\u8981\u7684\u89d2\u8272\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u5b9a\u4e49\u548c\u4f20\u9012\u8981\u5bf9\u6570\u636e\u6267\u884c\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u4ec0\u4e48\u662f\u51fd\u6570\u5f0f\u63a5\u53e3\uff1f<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u662f\u4e00\u4e2a\u5177\u6709\u5355\u4e2a\u62bd\u8c61\u65b9\u6cd5\u7684 Java \u754c\u9762\u3002\u8fd9\u4f7f\u5f97\u5b83\u53ef\u4ee5\u50cf\u51fd\u6570\u4e00\u6837\u4f20\u9012\u548c\u4f7f\u7528\uff0c\u800c\u4e0d\u5fc5\u521b\u5efa\u548c\u5b9e\u4f8b\u5316\u5b9e\u73b0\u8be5\u754c\u9762\u7684\u7c7b\u3002<\/p>\n<p><strong>\u6d41\u5904\u7406\u4e2d\u7684\u51fd\u6570\u5f0f\u63a5\u53e3<\/strong><\/p>\n<p>\u5728 Java 8 \u4e2d\u5f15\u5165\u4e86 Stream API\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ec4\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u7528\u4e8e\u5b9a\u4e49\u548c\u6267\u884c\u6d41\u5904\u7406\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li><strong>Predicate&lt;T&gt;\uff1a<\/strong>\u7528\u4e8e\u8fc7\u6ee4\u6d41\u4e2d\u7684\u5143\u7d20\u3002<\/li>\n<li><strong>Function&lt;T, R&gt;\uff1a<\/strong>\u7528\u4e8e\u5c06\u6d41\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u3002<\/li>\n<li><strong>Consumer&lt;T&gt;\uff1a<\/strong>\u7528\u4e8e\u5bf9\u6d41\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u64cd\u4f5c\u3002<\/li>\n<li><strong>Supplier&lt;T&gt;\uff1a<\/strong>\u7528\u4e8e\u521b\u5efa\u6d41\u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u5f0f\u63a5\u53e3\u6765\u5904\u7406\u6570\u636e\u6d41\uff1a<\/p>\n<pre>import java.util.Arrays;\nimport java.util.List;\nimport java.util.stream.Stream;\n\npublic class Main {\n\n  public static void main(String[] args) {\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5217\u8868\n    List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);\n\n    \/\/ \u4f7f\u7528 Stream API \u521b\u5efa\u4e00\u4e2a\u6d41\n    Stream&lt;Integer&gt; stream = numbers.stream();\n\n    \/\/ \u4f7f\u7528 filter() \u51fd\u6570\u5f0f\u63a5\u53e3\u8fc7\u6ee4\u51fa\u5076\u6570\n    Stream&lt;Integer&gt; evenNumbers = stream.filter((number) -&gt; number % 2 == 0);\n\n    \/\/ \u4f7f\u7528 map() \u51fd\u6570\u5f0f\u63a5\u53e3\u5c06\u6bcf\u4e2a\u5143\u7d20\u6620\u5c04\u4e3a\u5176\u5e73\u65b9\n    Stream&lt;Integer&gt; squaredNumbers = evenNumbers.map((number) -&gt; number * number);\n\n    \/\/ \u4f7f\u7528 forEach() \u51fd\u6570\u5f0f\u63a5\u53e3\u904d\u5386\u5e76\u6253\u5370\u7ed3\u679c\n    squaredNumbers.forEach(System.out::println);\n  }\n}<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>4\n8\n16\n20\n24\n28\n32<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>filter()<\/code> \u51fd\u6570\u5f0f\u63a5\u53e3\u8fc7\u6ee4\u51fa\u4e86\u5076\u6570\uff0c\u4f7f\u7528 <code>map()<\/code> \u51fd\u6570\u5f0f\u63a5\u53e3\u8ba1\u7b97\u4e86\u6bcf\u4e2a\u5076\u6570\u7684\u5e73\u65b9\uff0c\u6700\u540e\u4f7f\u7528 <code>forEach()<\/code> \u51fd\u6570\u5f0f\u63a5\u53e3\u904d\u5386\u5e76\u6253\u5370\u4e86\u7ed3\u679c\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6d41\u5904\u7406\u4e2d\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5b83\u4eec\u5141\u8bb8\u5f00\u53d1\u8005\u8f7b\u677e\u5730\u5b9a\u4e49\u548c\u6267\u884c\u590d\u6742\u7684\u6d41\u5904\u7406\u64cd\u4f5c\u3002\u901a\u8fc7\u5c06\u51fd\u6570\u5f0f\u63a5\u53e3\u4e0e Stream API \u76f8\u7ed3\u5408\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u7f16\u5199\u7b80\u6d01\u3001\u9ad8\u6548\u7684\u4ee3\u7801\u6765\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aJava\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6d41\u5904\u7406\u4e2d\u7684\u8fd0\u7528\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8ejava,\u51fd\u6570\u5f0f\u63a5\u53e3\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5f0f\u63a5\u53e3\u5728\u6d41\u5904\u7406\u4e2d\u7684\u8fd0&#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-203018","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203018","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=203018"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203018\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}