{"id":202746,"date":"2025-05-22T08:34:45","date_gmt":"2025-05-22T00:34:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202746\/"},"modified":"2025-05-22T08:34:45","modified_gmt":"2025-05-22T00:34:45","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e4%b8%ad%e9%ab%98%e9%98%b6%e5%87%bd%e6%95%b0%e7%9a%84%e5%8f%82%e6%95%b0%e5%92%8c%e8%bf%94%e5%9b%9e%e5%80%bc%e7%b1%bb%e5%9e%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202746\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\uff1f<\/h1>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729938461671cc41d884bf.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u9ad8\u9636\u51fd\u6570\uff1a\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b<\/strong><\/p>\n<p>\u5728 Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u9ad8\u9636\u51fd\u6570\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4ee5\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\u3002\u8981\u58f0\u660e\u9ad8\u9636\u51fd\u6570\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u6cdb\u578b\u7c7b\u578b\u53d8\u91cf\u6765\u8868\u793a\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002<\/p>\n<p><strong>\u53c2\u6570\u7c7b\u578b<\/strong><\/p>\n<p>\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u6307\u5b9a\u4e86\u9ad8\u9636\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u7684\u51fd\u6570\u7c7b\u578b\u3002\u5b83\u7531\u51fd\u6570\u7684\u8f93\u5165\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u7c7b\u578b\u7ec4\u6210\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u9ad8\u9636\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a IntFunction\uff0c\u8be5 IntFunction \u63a5\u53d7\u4e00\u4e2a int \u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a int\uff1a<\/p>\n<pre>public static int applyIntFunction(IntFunction&lt;Integer&gt; function, int input) {\n    return function.apply(input);\n}<\/pre>\n<p><strong>\u8fd4\u56de\u503c\u7c7b\u578b<\/strong><\/p>\n<p>\u9ad8\u9636\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u6307\u5b9a\u4e86\u51fd\u6570\u8fd4\u56de\u7684\u51fd\u6570\u7c7b\u578b\u3002\u5b83\u7531\u51fd\u6570\u7684\u8f93\u5165\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u7c7b\u578b\u7ec4\u6210\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u9ad8\u9636\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a Function\uff0c\u8be5 Function \u63a5\u53d7\u4e00\u4e2a String \u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a int\uff1a<\/p>\n<pre>public static Function&lt;String, Integer&gt; createToIntFunction(String s) {\n    return x -&gt; x.length() + s.length();\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9ad8\u9636\u51fd\u6570\u5bf9\u5b57\u7b26\u4e32\u96c6\u5408\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\u67e5\u627e\u6700\u957f\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>List&lt;String&gt; strings = List.of(\"Hello\", \"World\", \"Functional\", \"Programming\");\n\n\/\/ \u4f7f\u7528 reduce \u67e5\u627e\u6700\u957f\u7684\u5b57\u7b26\u4e32\nOptional&lt;String&gt; longestString = strings.stream()\n        .reduce((prev, current) -&gt; prev.length() &gt; current.length() ? prev : current);\n\nSystem.out.println(longestString.get()); \/\/ \u8f93\u51fa \"Functional\"<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0creduce \u64cd\u4f5c\u4f7f\u7528\u4e86\u9ad8\u9636\u51fd\u6570 Comparator.comparing\uff0c\u63a5\u53d7\u4e00\u4e2a String \u5e76\u8fd4\u56de\u4e00\u4e2a int\uff08\u5b57\u7b26\u4e32\u957f\u5ea6\uff09\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u7684\u53c2&#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-202746","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202746","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=202746"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202746\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}