{"id":202922,"date":"2025-05-22T09:39:44","date_gmt":"2025-05-22T01:39:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202922\/"},"modified":"2025-05-22T09:39:44","modified_gmt":"2025-05-22T01:39:44","slug":"%e5%87%bd%e6%95%b0%e5%bc%8f%e6%8e%a5%e5%8f%a3%e5%9c%a8java%e5%bc%80%e5%8f%91%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8%e5%9c%ba%e6%99%af%e6%9c%89%e5%93%aa%e4%ba%9b%ef%bc%9f%e5%a6%82%e4%bd%95%e6%9c%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202922\/","title":{"rendered":"\u51fd\u6570\u5f0f\u63a5\u53e3\u5728Java\u5f00\u53d1\u4e2d\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f\u5982\u4f55\u6709\u6548\u4f7f\u7528\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u51fd\u6570\u5f0f\u63a5\u53e3\u5728Java\u5f00\u53d1\u4e2d\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f\u5982\u4f55\u6709\u6548\u4f7f\u7528\uff1f<\/h1>\n<p>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u51fd\u6570\u5f0f\u63a5\u53e3\u5728Java\u5f00\u53d1\u4e2d\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f\u5982\u4f55\u6709\u6548\u4f7f\u7528\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u5728 Java \u4e2d\u5e94\u7528\u5e7f\u6cdb\uff0c\u7528\u4e8e\u56de\u8c03\u51fd\u6570\u3001\u4e8b\u4ef6\u5904\u7406\u548c\u6570\u636e\u5904\u7406\u3002\u5177\u4f53\u5e94\u7528\u573a\u666f\u5305\u62ec\uff1a\u56de\u8c03\u51fd\u6570\uff1a\u901a\u8fc7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u8868\u793a\u56de\u8c03\u51fd\u6570\uff0c\u4f7f\u4ee3\u7801\u7b80\u6d01\u6613\u7528\u3002\u4e8b\u4ef6\u5904\u7406\uff1a\u5b9a\u4e49\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u63d0\u5347\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u6570\u636e\u5904\u7406\uff1a\u5229\u7528 Stream API \u548c\u96c6\u5408\u6846\u67b6\u7684\u64cd\u4f5c\uff0c\u4f7f\u4ee3\u7801\u7b80\u6d01\u9ad8\u6548\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729998649671daf399522d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u51fd\u6570\u5f0f\u63a5\u53e3\u5728 Java \u5f00\u53d1\u4e2d\u7684\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u662f\u6307\u4ec5\u5305\u542b\u5355\u4e2a\u62bd\u8c61\u65b9\u6cd5\u7684\u63a5\u53e3\u3002\u5b83\u4eec\u5728 Java \u5f00\u53d1\u4e2d\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<p><strong>1. \u56de\u8c03\u51fd\u6570<\/strong><\/p>\n<p>\u56de\u8c03\u51fd\u6570\u662f\u4ee5\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u51fd\u6570\u3002\u51fd\u6570\u5f0f\u63a5\u53e3\u53ef\u4ee5\u5f88\u597d\u5730\u8868\u793a\u56de\u8c03\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4eec\u7b80\u6d01\u4e14\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u5b9a\u4e49\u4e00\u4e2a\u63a5\u6536\u6574\u6570\u5e76\u8fd4\u56de\u5176\u5e73\u65b9\u7684\u56de\u8c03\u51fd\u6570\nFunction&lt;Integer, Integer&gt; squareFunction = x -&gt; x * x;\n\n\/\/ \u4f7f\u7528\u56de\u8c03\u51fd\u6570\u6765\u8ba1\u7b97\u4e00\u4e2a\u5217\u8868\u7684\u5e73\u65b9\u548c\nint sumOfSquares = numbers.stream()\n        .map(squareFunction)\n        .reduce(0, (x, y) -&gt; x + y);<\/pre>\n<p><strong>2. \u4e8b\u4ef6\u5904\u7406<\/strong><\/p>\n<p>GUI \u5e94\u7528\u7a0b\u5e8f\u548c\u7f51\u7edc\u670d\u52a1\u5668\u4f7f\u7528\u4e8b\u4ef6\u6765\u54cd\u5e94\u7528\u6237\u4ea4\u4e92\u548c\u7cfb\u7edf\u72b6\u6001\u7684\u53d8\u5316\u3002\u51fd\u6570\u5f0f\u63a5\u53e3\u53ef\u4ee5\u5b9a\u4e49\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4e3a\u6309\u94ae\u5b9a\u4e49\u4e00\u4e2a\u70b9\u51fb\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\nbutton.setOnAction(e -&gt; {\n    \/\/ \u5904\u7406\u70b9\u51fb\u4e8b\u4ef6\n});<\/pre>\n<p><strong>3. \u6570\u636e\u5904\u7406<\/strong><\/p>\n<p>Stream API \u548c\u96c6\u5408\u6846\u67b6\u5e7f\u6cdb\u4f7f\u7528\u51fd\u6570\u5f0f\u63a5\u53e3\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u51fd\u6570\u5f0f\u63a5\u53e3\u8868\u793a\u8f6c\u6362\u3001\u8fc7\u6ee4\u548c\u805a\u5408\u64cd\u4f5c\uff0c\u4f7f\u4ee3\u7801\u7b80\u6d01\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4f7f\u7528 Stream API \u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5947\u6570\nList&lt;Integer&gt; oddNumbers = numbers.stream()\n        .filter(x -&gt; x % 2 != 0)\n        .toList();<\/pre>\n<p><strong>\u6709\u6548\u4f7f\u7528\u51fd\u6570\u5f0f\u63a5\u53e3\u7684\u6280\u5de7<\/strong><\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u6807\u51c6\u529f\u80fd\u63a5\u53e3\uff1a<\/strong> Java API \u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\u5f0f\u63a5\u53e3\uff08\u5982 <code>Predicate<\/code>\u3001<code>Function<\/code> \u548c <code>Consumer<\/code>\uff09\uff0c\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u3002<\/li>\n<li><strong>\u4fdd\u6301\u63a5\u53e3\u7b80\u6d01\uff1a<\/strong> \u51fd\u6570\u5f0f\u63a5\u53e3\u5e94\u8be5\u53ea\u5305\u542b\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\uff0c\u5177\u4f53\u5b9e\u73b0\u7559\u7ed9\u5b9e\u73b0\u7c7b\u3002<\/li>\n<li><strong>\u5229\u7528 lambda \u8868\u8fbe\u5f0f\uff1a<\/strong> lambda \u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u5728\u65e0\u9700\u521b\u5efa\u5355\u72ec\u7c7b\u7684\u60c5\u51b5\u4e0b\u5b9a\u4e49\u51fd\u6570\u5f0f\u63a5\u53e3\u5b9e\u73b0\u7684\u7b80\u4fbf\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u4f7f\u7528\u65b9\u6cd5\u5f15\u7528\uff1a<\/strong> \u65b9\u6cd5\u5f15\u7528\u662f lambda \u8868\u8fbe\u5f0f\u7684\u66ff\u4ee3\u8bed\u6cd5\uff0c\u5f53\u5b9e\u73b0\u65b9\u6cd5\u5df2\u7ecf\u5b58\u5728\u65f6\uff0c\u53ef\u4ee5\u51cf\u5c11\u4ee3\u7801\u91cf\u3002<\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u51fd\u6570\u5f0f\u63a5\u53e3\u5728Java\u5f00\u53d1\u4e2d\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f\u5982\u4f55\u6709\u6548\u4f7f\u7528\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u5728Java\u5f00\u53d1\u4e2d\u7684\u5e94\u7528&#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-202922","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202922","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=202922"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202922\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}