{"id":327,"date":"2023-02-25T06:21:57","date_gmt":"2023-02-24T22:21:57","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=327"},"modified":"2023-04-30T14:52:48","modified_gmt":"2023-04-30T06:52:48","slug":"java8-new-features-function-and-bifunction","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/java8-new-features-function-and-bifunction\/","title":{"rendered":"Java8\u65b0\u7279\u6027Function\u548cBiFunction"},"content":{"rendered":"<h2>Function<\/h2>\n<h3>Function\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u4e3b\u8981\u65b9\u6cd5apply\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u503c<\/h3>\n<pre><code class=\"language-java\">@FunctionalInterface\npublic interface Function&lt;T, R&gt; {\n\n    \/**\n     * Applies this function to the given argument.\n     *\n     * @param t the function argument\n     * @return the function result\n     *\/\n    R apply(T t);\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u9996\u5148\u6211\u4eec\u6765\u5199\u4e00\u4e2a\u8ba1\u7b97\u6570\u5b57\u7684\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">public int compute(int a, Function&lt;Integer, Integer&gt; function) {\n    int result = function.apply(a);\n    return result;\n}<\/code><\/pre>\n<p>\u7136\u540e\u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">test.compute(5, value -&gt; value * value) \/\/25 \u8ba1\u7b97\u5e73\u65b9\ntest.compute(5, value -&gt; value + value) \/\/10 \u6c42\u548c\ntest.compute(5, value -&gt; value - 2) \/\/3<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\u5c31\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u529f\u80fd\uff0c\u8fd9\u5c31\u662f\u524d\u9762\u8bf4\u8fc7\u7684Lambda\u8868\u8fbe\u5f0f\u4f20\u9012\u7684\u662f\u4e00\u79cd\u884c\u4e3a\uff0c\u6211\u4eec\u628a\u60f3\u8981\u505a\u7684\u4e8b\u5728\u8c03\u7528\u7684\u65f6\u5019\uff0c\u4ee5\u4e00\u79cd\u884c\u4e3a\u7684\u65b9\u5f0f\u4f20\u9012\u8fdb\u6765\uff0c\u7a0b\u5e8f\u8bfb\u8d77\u6765\u4e5f\u66f4\u52a0\u76f4\u89c2<\/p>\n<h3>Function compose\u65b9\u6cd5<\/h3>\n<p>compose\u65b9\u6cd5\u662f\u4e00\u4e2a\u9ed8\u8ba4\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u6536\u4e00\u4e2afunction\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u53c2\u6570function\u6267\u884c\u7684\u7ed3\u679c\u4f5c\u4e3a\u53c2\u6570\u7ed9\u8c03\u7528\u7684function\uff0c\u4ee5\u6b64\u6765\u5b9e\u73b0\u4e24\u4e2afunction\u7ec4\u5408\u7684\u529f\u80fd\u3002<\/p>\n<pre><code class=\"language-java\">public interface Function&lt;T, R&gt; {\n    default &lt;V&gt; Function&lt;V, R&gt; compose(Function&lt;? super V, ? extends T&gt; before) {\n        Objects.requireNonNull(before);\n        return (V v) -&gt; apply(before.apply(v));\n    }\n}<\/code><\/pre>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u4e3e\u4f8b\u770b\u4e00\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public int compute(int a, Function&lt;Integer, Integer&gt; function1, Function&lt;Integer, Integer&gt; function2) {\n    return function1.compose(function2).apply(a);\n}<\/code><\/pre>\n<p>\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">test.compute(2, value -&gt; value * 3, value -&gt; value * value)<\/code><\/pre>\n<p>\u5927\u5bb6\u53ef\u4ee5\u731c\u4e00\u4e0b\u4e0a\u9762\u7684\u7ed3\u679c\u662f\u591a\u5c11\uff1f<\/p>\n<p>\u6211\u4eec\u6765\u5206\u6790\u4e00\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">function1.compose(function2).apply(a);<\/code><\/pre>\n<p>compose\u65b9\u6cd5\u5185\u90e8\u4ee3\u7801\u662f\uff1a<\/p>\n<pre><code class=\"language-java\">return (V v) -&gt; apply(before.apply(v));<\/code><\/pre>\n<p>\u8fd4\u56de\u7684\u662f\u4e00\u4e2aFunction\uff0c\u8f93\u5165\u4e00\u4e2a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u53c2\u6570\u503c\uff0c\u8fd9\u4e2aFunction \u5728\u8c03\u7528apply\u65f6\u9996\u5148\u6267\u884c\u7684\u662f<code>before.apply(v)<\/code>\uff0cbefore\u5728\u8fd9\u91cc\u5c31\u662f<code>value -&gt; value * value<\/code>\uff0c\u4e5f\u5c31\u662f 2*2\uff0c\u5c06\u5f97\u5230\u7684\u7ed3\u679c4\uff0c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9function1\uff0c\u5728\u8fd9\u91cc\u5c31\u662f<code>value -&gt; value * 3<\/code>\uff0c\u6240\u4ee5\u7ed3\u679c\u662f\uff1a12<\/p>\n<h3>Function andThen<\/h3>\n<p>\u4e86\u89e3\u4e86compose\u65b9\u6cd5\uff0c\u6211\u4eec\u518d\u6765\u770bandThen\u65b9\u6cd5\u5c31\u597d\u7406\u89e3\u4e86\uff0c\u542c\u540d\u5b57\u5c31\u662f\u201c\u63a5\u4e0b\u6765\u201d\uff0candThen\u65b9\u6cd5\u4e5f\u662f\u63a5\u6536\u4e00\u4e2afunction\u4f5c\u4e3a\u53c2\u6570\uff0c\u4e0ecompse\u4e0d\u540c\u7684\u662f\uff0c\u5148\u6267\u884c\u672c\u8eab\u7684apply\u65b9\u6cd5\uff0c\u5c06\u6267\u884c\u7684\u7ed3\u679c\u4f5c\u4e3a\u53c2\u6570\u7ed9\u53c2\u6570\u4e2d\u7684function\u3002<\/p>\n<pre><code class=\"language-java\">public interface Function&lt;T, R&gt; {\n    default &lt;V&gt; Function&lt;T, V&gt; andThen(Function&lt;? super R, ? extends V&gt; after) {\n        Objects.requireNonNull(after);\n        return (T t) -&gt; after.apply(apply(t));\n    }\n}<\/code><\/pre>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u4e3e\u4f8b\u770b\u4e00\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public int compute2(int a, Function&lt;Integer, Integer&gt; function1, Function&lt;Integer, Integer&gt; function2) {\n    return function1.andThen(function2).apply(a);\n}<\/code><\/pre>\n<p>\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">test.compute2(2, value -&gt; value * 3, value -&gt; value * value)<\/code><\/pre>\n<p>\u8fd9\u6b21\u7ed3\u679c\u662f\u591a\u5c11\u5462\uff1f\u6211\u60f3\u5927\u5bb6\u5e94\u8be5\u5f88\u5bb9\u6613\u5c31\u77e5\u9053\u4e86\uff0c\u9996\u5148\u6267\u884c\u7684\u662f<code>value -&gt; value * 3<\/code>\u7ed3\u679c\u662f6\uff0c\u7136\u540e\u6267\u884c\u7684\u662f<code>value * value<\/code>\u6700\u7ec8\u7ed3\u679c\u662f36<\/p>\n<h2>BiFunction<\/h2>\n<p>\u4e86\u89e3\u4e86Function\u4ee5\u540e\uff0c\u6709\u7684\u5c0f\u4f19\u4f34\u53ef\u80fd\u4f1a\u95ee\u4e86\uff0cFunction\u53ea\u80fd\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u5982\u679c\u6211\u8981\u4f20\u9012\u4e24\u4e2a\u53c2\u6570\u5462\uff0c\u8fd9\u4e00\u70b9Java8\u4e5f\u66ff\u6211\u4eec\u8003\u8651\u5230\u4e86\uff0c\u5c31\u662f\u6211\u4eec\u622a\u4e0b\u6765\u8981\u8bb2\u5230\u7684 BiFunction\uff0c\u9996\u5148\u8fd8\u662f\u76f4\u63a5\u4e0a\u6e90\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">@FunctionalInterface\npublic interface BiFunction&lt;T, U, R&gt; {\n    R apply(T t, U u);\n\n    default &lt;V&gt; BiFunction&lt;T, U, V&gt; andThen(Function&lt;? super R, ? extends V&gt; after) {\n        Objects.requireNonNull(after);\n        return (T t, U u) -&gt; after.apply(apply(t, u));\n    }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230BiFunction\u7684apply\u65b9\u6cd5\uff0c\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50<\/p>\n<pre><code class=\"language-java\">public int compute3(int a, int b, BiFunction&lt;Integer, Integer, Integer&gt; biFunction) {\n   return biFunction.apply(a, b);\n}<\/code><\/pre>\n<p>\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u5f88\u591a\u8fd0\u7b97<\/p>\n<pre><code class=\"language-java\">test.compute3(2, 3, (v1, v2) -&gt; v1 + v2) \/\/5\ntest.compute3(2, 3, (v1, v2) -&gt; v1 - v2) \/\/-1\ntest.compute3(2, 3, (v1, v2) -&gt; v1 * v2) \/\/6<\/code><\/pre>\n<p>\u8fd9\u4e2a\u8fd8\u662f\u86ee\u7b80\u5355\u7684\uff0c\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e0b\uff0cBiFunction\u4e2d\u6709\u4e00\u4e2aandThen\u65b9\u6cd5\uff0c\u53c2\u6570\u662f\u4e00\u4e2aFunction\uff0c\u65b9\u6cd5\u4e3b\u8981\u662f\u5c06BiFunction\u8fd4\u56de\u7684\u7ed3\u679c\u4f5c\u4e3aFunction\u7684\u53c2\u6570\uff0c\u5f97\u51fa\u4e00\u4e2a\u7ed3\u679c\uff0c\u4e3e\u4f8b\uff1a<\/p>\n<pre><code class=\"language-java\">public int compute4(int a, int b, BiFunction&lt;Integer, Integer, Integer&gt; biFunction, Function&lt;Integer, Integer&gt; function) {\n    return biFunction.andThen(function).apply(a, b);\n}\ntest.compute4(2, 3, (v1, v2) -&gt; v1 + v2, v1 -&gt; v1 * v1)<\/code><\/pre>\n<p>\u9996\u5148\u6267\u884c<code>(v1, v2) -&gt; v1 + v2<\/code>\uff0c\u7136\u540e\u6267\u884c<code>v1 -&gt; v1 * v1<\/code><\/p>\n<p>\u6709\u7684\u540c\u5b66\u53ef\u80fd\u4f1a\u95ee\u4e3a\u4ec0\u4e48BiFunction\u6ca1\u6709compose\u65b9\u6cd5\u5462\uff0c\u5927\u5bb6\u4ed4\u7ec6\u60f3\u4e00\u60f3\uff0c\u5982\u679c\u6709compose\u65b9\u6cd5\u7684\u8bdd\uff0c\u90a3\u5c31\u662f\u5148\u6267\u884cFunction\u7684apply\u65b9\u6cd5\uff0c\u4f46\u662f\u6267\u884c\u5b8c\u6bd5\u540e\u53ea\u8fd4\u56de\u4e00\u4e2a\u53c2\u6570\uff0c\u800cBiFunction\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u6240\u4ee5\u80af\u5b9a\u662f\u4e0d\u884c\u7684\u3002<\/p>\n<h2>\u4ee3\u7801\u6848\u4f8b<\/h2>\n<pre><code class=\"language-java\">public class FunctionTest {\n\n    public static void main(String[] args) {\n        FunctionTest test = new FunctionTest();\n        System.out.println(test.compute(1, a -&gt; a + 1));\n\n        \/\/ \u666e\u901a\u5b9a\u4e49\n        Function&lt;Integer, Integer&gt; function = new Function&lt;Integer, Integer&gt;() {\n            @Override\n            public Integer apply(Integer o) {\n                return o * o;\n            }\n        };\n        Function&lt;Integer, String&gt; function1 = function.andThen(new Function&lt;Integer, String&gt;() {\n            @Override\n            public String apply(Integer integer) {\n                return &quot;result is &quot; + integer;\n            }\n        });\n\n        String apply = function1.apply(2);\n        System.out.println(apply);\n        \/\/ \u7528lambda\u7684\u65b9\u5f0f\u5b9e\u73b0\n        Function&lt;Integer, Integer&gt; lambdaFunc = o -&gt; o * o;\n        Function&lt;Integer, String&gt; function2 = lambdaFunc.andThen(o -&gt; &quot;result is &quot; + o);\n        String apply1 = function2.apply(2);\n        System.out.println(apply1);\n    }\n\n    public int compute(int a, Function&lt;Integer, Integer&gt; function) {\n        return function.apply(a);\n    }\n}<\/code><\/pre>\n<pre><code>2\nresult is 4\nresult is 4<\/code><\/pre>\n<pre><code class=\"language-java\">public class BiFunctionTest {\n\n    public static void main(String[] args) {\n        Person zhangsan = new Person(20, &quot;zhangsan&quot;);\n        Person lisi = new Person(30, &quot;lisi&quot;);\n        Person wangwu = new Person(40, &quot;wangwu&quot;);\n\n        List&lt;Person&gt; personList = new ArrayList&lt;&gt;();\n        personList.add(zhangsan);\n        personList.add(lisi);\n        personList.add(wangwu);\n\n        BiFunctionTest test = new BiFunctionTest();\n        List&lt;Person&gt; personByName = test.getPersonByName(&quot;zhangsan&quot;, personList);\n        List&lt;Person&gt; personByAge = test.getPersonByAge(20, personList);\n        System.out.println(personByName);\n        System.out.println(personByAge);\n\n        List&lt;Person&gt; personByAge2 = test.getPersonByAge2(20, personList, (age, list) -&gt; list.stream()\n                .filter(p -&gt; p.getAge() &gt; age).collect(Collectors.toList()));\n        System.out.println(personByAge2);\n    }\n\n    public List&lt;Person&gt; getPersonByName(String name, List&lt;Person&gt; personList) {\n        return personList.stream().filter(person -&gt; person.getName().equals(name)).collect(Collectors.toList());\n    }\n\n    public List&lt;Person&gt; getPersonByAge(int age, List&lt;Person&gt; personList) {\n        BiFunction&lt;Integer, List&lt;Person&gt;, List&lt;Person&gt;&gt; biFunction\n                = (ageOfPerson, persons) -&gt; persons.stream()\n                .filter(person -&gt; person.getAge() &gt; 20)\n                .collect(Collectors.toList());\n        return biFunction.apply(age, personList);\n    }\n\n    \/\/ \u66f4\u52a0\u7075\u6d3b\u7684\u65b9\u5f0f \u8ba9\u8c03\u7528\u8005\u5b9e\u73b0\u8fc7\u6ee4\u7684\u6761\u4ef6 \u662f\u5927\u4e8e\u8fd8\u662f\u5c0f\u4e8e\n    public List&lt;Person&gt; getPersonByAge2(int age, List&lt;Person&gt; personList, BiFunction&lt;Integer, List&lt;Person&gt;, List&lt;Person&gt;&gt; biFunction) {\n        return biFunction.apply(age, personList);\n    }\n}<\/code><\/pre>\n<pre><code>[Person(age=20, name=zhangsan)]\n[Person(age=30, name=lisi), Person(age=40, name=wangwu)]\n[Person(age=30, name=lisi), Person(age=40, name=wangwu)]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Function Function\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u4e3b\u8981\u65b9\u6cd5apply\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u503c @Funct [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-327","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=327"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/327\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}