{"id":823,"date":"2025-01-06T22:38:17","date_gmt":"2025-01-06T14:38:17","guid":{"rendered":"https:\/\/www.ikaros.love\/?p=823"},"modified":"2025-01-06T22:38:17","modified_gmt":"2025-01-06T14:38:17","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e5%92%8c%e5%87%bd%e6%95%b0%e5%bc%8f%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/www.ikaros.love\/?p=823","title":{"rendered":"java\u51fd\u6570\u5f0f\u7f16\u7a0b\u548c\u51fd\u6570\u5f0f\u63a5\u53e3"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"484\" height=\"484\" src=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2025\/01\/85.png\" alt=\"\" class=\"wp-image-824\" srcset=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2025\/01\/85.png 484w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2025\/01\/85-300x300.png 300w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2025\/01\/85-150x150.png 150w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2025\/01\/85-200x200.png 200w\" sizes=\"(max-width: 484px) 100vw, 484px\" \/><\/figure>\n\n\n\n<p>\u4e0a\u8ff0\u7684\u51fd\u6570\u5f0f\u63a5\u53e3\u90fd\u5f52\u5c5e\u4e8e Java \u7684 <strong>\u51fd\u6570\u5f0f\u7f16\u7a0b\uff08Functional Programming\uff09<\/strong> \u548c <strong>\u51fd\u6570\u5f0f\u63a5\u53e3\uff08Functional Interfaces\uff09<\/strong> \u90e8\u5206\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5b83\u4eec\u4f4d\u4e8e Java 8 \u5f15\u5165\u7684 <code>java.util.function<\/code> \u5305\u4e2d\uff0c\u65e8\u5728\u63d0\u4f9b\u4e00\u7ec4\u6807\u51c6\u7684\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u4ee5\u4fbf\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\u4e2d\u4f7f\u7528\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u51fd\u6570\u5f0f\u7f16\u7a0b\u548c\u51fd\u6570\u5f0f\u63a5\u53e3<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u51fd\u6570\u5f0f\u7f16\u7a0b<\/h4>\n\n\n\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5f3a\u8c03\u4f7f\u7528\u6570\u5b66\u51fd\u6570\u7684\u6982\u5ff5\u6765\u6784\u5efa\u8ba1\u7b97\u673a\u7a0b\u5e8f\u3002\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002Java 8 \u5f15\u5165\u4e86\u5bf9\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u652f\u6301\uff0c\u5305\u62ec lambda \u8868\u8fbe\u5f0f\u3001\u65b9\u6cd5\u5f15\u7528\u548c\u51fd\u6570\u5f0f\u63a5\u53e3\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u7559\u7a7a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u51fd\u6570\u5f0f\u63a5\u53e3<\/h4>\n\n\n\n<p>\u51fd\u6570\u5f0f\u63a5\u53e3\u662f Java \u4e2d\u7684\u4e00\u79cd\u7279\u6b8a\u63a5\u53e3\uff0c\u5b83\u53ea\u6709\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\uff08\u5373 SAM\uff0cSingle Abstract Method\uff09\u3002Java 8 \u5f15\u5165\u4e86 <code>@FunctionalInterface<\/code> \u6ce8\u89e3\uff0c\u7528\u4e8e\u6807\u8bb0\u8fd9\u6837\u7684\u63a5\u53e3\u3002\u51fd\u6570\u5f0f\u63a5\u53e3\u53ef\u4ee5\u88ab lambda \u8868\u8fbe\u5f0f\u9690\u5f0f\u5730\u5b9e\u4f8b\u5316\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>java.util.function<\/code> \u5305<\/h3>\n\n\n\n<p><code>java.util.function<\/code> \u5305\u63d0\u4f9b\u4e86\u8bb8\u591a\u5e38\u7528\u7684\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u8fd9\u4e9b\u63a5\u53e3\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u51fd\u6570\u5f0f\u7f16\u7a0b\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u7684\u5305\u548c\u63a5\u53e3\uff1a<\/p>\n\n\n\n<ul>\n<li><strong><code>java.util.function<\/code> \u5305<\/strong>\uff1a<\/li>\n\n\n\n<li>\u5305\u542b\u4e86\u8bb8\u591a\u9884\u5b9a\u4e49\u7684\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u5982 <code>Function<\/code>\u3001<code>BiFunction<\/code>\u3001<code>Consumer<\/code>\u3001<code>BiConsumer<\/code>\u3001<code>Supplier<\/code>\u3001<code>Predicate<\/code>\u3001<code>BiPredicate<\/code>\u3001<code>UnaryOperator<\/code> \u548c <code>BinaryOperator<\/code>\u3002<\/li>\n\n\n\n<li>\u8fd9\u4e9b\u63a5\u53e3\u7684\u8bbe\u8ba1\u76ee\u7684\u662f\u4e3a\u4e86\u7b80\u5316 lambda \u8868\u8fbe\u5f0f\u7684\u4f7f\u7528\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u89c1\u7684\u51fd\u6570\u5f0f\u63a5\u53e3\u53ca\u5176\u7528\u9014<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. <code>Function&lt;T, R&gt;<\/code><\/h4>\n\n\n\n<ul>\n<li><strong>\u7528\u9014<\/strong>\uff1a\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><strong>\u4e3b\u8981\u65b9\u6cd5<\/strong>\uff1a<code>R apply(T t)<\/code><\/li>\n\n\n\n<li><strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  Function&lt;String, Integer&gt; stringToLength = String::length;\n  int length = stringToLength.apply(\"Hello\"); \/\/ \u7ed3\u679c\u662f 5<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. <code>BiFunction&lt;T, U, R&gt;<\/code><\/h4>\n\n\n\n<ul>\n<li><strong>\u7528\u9014<\/strong>\uff1a\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><strong>\u4e3b\u8981\u65b9\u6cd5<\/strong>\uff1a<code>R apply(T t, U u)<\/code><\/li>\n\n\n\n<li><strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  BiFunction&lt;Integer, Integer, Integer&gt; adder = (a, b) -&gt; a + b;\n  int result = adder.apply(3, 4); \/\/ \u7ed3\u679c\u662f 7<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. <code>Consumer&lt;T&gt;<\/code><\/h4>\n\n\n\n<ul>\n<li><strong>\u7528\u9014<\/strong>\uff1a\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u4e0d\u8fd4\u56de\u4efb\u4f55\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><strong>\u4e3b\u8981\u65b9\u6cd5<\/strong>\uff1a<code>void accept(T t)<\/code><\/li>\n\n\n\n<li><strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  Consumer&lt;String&gt; printString = System.out::println;\n  printString.accept(\"Hello\"); \/\/ \u8f93\u51fa: Hello<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4. <code>BiConsumer&lt;T, U&gt;<\/code><\/h4>\n\n\n\n<ul>\n<li><strong>\u7528\u9014<\/strong>\uff1a\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u4e0d\u8fd4\u56de\u4efb\u4f55\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><strong>\u4e3b\u8981\u65b9\u6cd5<\/strong>\uff1a<code>void accept(T t, U u)<\/code><\/li>\n\n\n\n<li><strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  BiConsumer&lt;String, Integer&gt; printMessage = (message, count) -&gt; {\n      for (int i = 0; i &lt; count; i++) {\n          System.out.println(message);\n      }\n  };\n  printMessage.accept(\"Hello\", 3); \/\/ \u8f93\u51fa: Hello\n                                   \/\/       Hello\n                                   \/\/       Hello<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5. <code>Supplier&lt;T&gt;<\/code><\/h4>\n\n\n\n<ul>\n<li><strong>\u7528\u9014<\/strong>\uff1a\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><strong>\u4e3b\u8981\u65b9\u6cd5<\/strong>\uff1a<code>T get()<\/code><\/li>\n\n\n\n<li><strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  Supplier&lt;Double&gt; randomSupplier = Math::random;\n  double randomValue = randomSupplier.get(); \/\/ \u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u6570<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6. <code>Predicate&lt;T&gt;<\/code><\/h4>\n\n\n\n<ul>\n<li><strong>\u7528\u9014<\/strong>\uff1a\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002<\/li>\n\n\n\n<li><strong>\u4e3b\u8981\u65b9\u6cd5<\/strong>\uff1a<code>boolean test(T t)<\/code><\/li>\n\n\n\n<li><strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  Predicate&lt;String&gt; isEmpty = String::isEmpty;\n  boolean result = isEmpty.test(\"\"); \/\/ \u7ed3\u679c\u662f true<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">7. <code>BiPredicate&lt;T, U&gt;<\/code><\/h4>\n\n\n\n<ul>\n<li><strong>\u7528\u9014<\/strong>\uff1a\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002<\/li>\n\n\n\n<li><strong>\u4e3b\u8981\u65b9\u6cd5<\/strong>\uff1a<code>boolean test(T t, U u)<\/code><\/li>\n\n\n\n<li><strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  BiPredicate&lt;Integer, Integer&gt; isEqual = (a, b) -&gt; a.equals(b);\n  boolean result = isEqual.test(3, 3); \/\/ \u7ed3\u679c\u662f true<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">8. <code>UnaryOperator&lt;T&gt;<\/code><\/h4>\n\n\n\n<ul>\n<li><strong>\u7528\u9014<\/strong>\uff1a\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u76f8\u540c\u7c7b\u578b\u7684\u53c2\u6570\u3002<\/li>\n\n\n\n<li><strong>\u4e3b\u8981\u65b9\u6cd5<\/strong>\uff1a<code>T apply(T t)<\/code><\/li>\n\n\n\n<li><strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  UnaryOperator&lt;Integer&gt; increment = x -&gt; x + 1;\n  int result = increment.apply(5); \/\/ \u7ed3\u679c\u662f 6<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">9. <code>BinaryOperator&lt;T&gt;<\/code><\/h4>\n\n\n\n<ul>\n<li><strong>\u7528\u9014<\/strong>\uff1a\u63a5\u53d7\u4e24\u4e2a\u76f8\u540c\u7c7b\u578b\u7684\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u76f8\u540c\u7c7b\u578b\u7684\u53c2\u6570\u3002<\/li>\n\n\n\n<li><strong>\u4e3b\u8981\u65b9\u6cd5<\/strong>\uff1a<code>T apply(T t, T u)<\/code><\/li>\n\n\n\n<li><strong>\u793a\u4f8b<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  BinaryOperator&lt;Integer&gt; adder = (a, b) -&gt; a + b;\n  int result = adder.apply(3, 4); \/\/ \u7ed3\u679c\u662f 7<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5904\u7406\u53ef\u53d8\u53c2\u6570<\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u53ef\u53d8\u53c2\u6570\uff08varargs\uff09\uff0cJava \u6807\u51c6\u51fd\u6570\u5f0f\u63a5\u53e3\u5e76\u4e0d\u76f4\u63a5\u652f\u6301\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>Function<\/code> \u6216\u81ea\u5b9a\u4e49\u51fd\u6570\u5f0f\u63a5\u53e3\u6765\u5904\u7406\u53ef\u53d8\u53c2\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>Function&lt;int[], Integer&gt;<\/code> \u5904\u7406\u53ef\u53d8\u53c2\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.function.Function;\n\npublic class Calculator {\n    public static int sum(int... numbers) {\n        int total = 0;\n        for (int number : numbers) {\n            total += number;\n        }\n        return total;\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        Function&lt;int&#91;], Integer&gt; sumFunction = Calculator::sum;\n        int result = sumFunction.apply(new int&#91;]{1, 2, 3, 4, 5}); \/\/ \u7ed3\u679c\u662f 15\n        System.out.println(\"Sum: \" + result);\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u51fd\u6570\u5f0f\u63a5\u53e3\u5904\u7406\u53ef\u53d8\u53c2\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>@FunctionalInterface\ninterface VarArgsFunction {\n    int apply(int... numbers);\n}\n\npublic class Calculator {\n    public static int sum(int... numbers) {\n        int total = 0;\n        for (int number : numbers) {\n            total += number;\n        }\n        return total;\n    }\n}\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        VarArgsFunction sumFunction = Calculator::sum;\n        int result = sumFunction.apply(1, 2, 3, 4, 5); \/\/ \u7ed3\u679c\u662f 15\n        System.out.println(\"Sum: \" + result);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u8ff0\u7684\u51fd\u6570\u5f0f\u63a5\u53e3\u90fd\u5f52\u5c5e\u4e8e Jav&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":824,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[147,213,215,214],"_links":{"self":[{"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/posts\/823"}],"collection":[{"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=823"}],"version-history":[{"count":1,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/posts\/823\/revisions"}],"predecessor-version":[{"id":825,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/posts\/823\/revisions\/825"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/media\/824"}],"wp:attachment":[{"href":"https:\/\/www.ikaros.love\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}