{"id":10006933,"date":"2022-11-29T14:11:44","date_gmt":"2022-11-29T06:11:44","guid":{"rendered":"https:\/\/ityet.com\/?p=10006933"},"modified":"2022-11-29T14:11:44","modified_gmt":"2022-11-29T06:11:44","slug":"function%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90%e4%b8%8e%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006933","title":{"rendered":"Function\u6e90\u7801\u89e3\u6790\u4e0e\u5b9e\u8df5"},"content":{"rendered":"<h2>\u4f5c\u8005\uff1a\u9648\u660c\u6d69<\/h2>\n<h3>1 \u5bfc\u8bfb<\/h3>\n<p>if\u2026else\u2026\u5728\u4ee3\u7801\u4e2d\u7ecf\u5e38\u4f7f\u7528\uff0c\u542c\u8bf4\u53ef\u4ee5\u901a\u8fc7Java 8\u7684Function\u63a5\u53e3\u6765\u6d88\u706dif\u2026else\u2026\uff01Function\u63a5\u53e3\u662f\u4ec0\u4e48\uff1f\u5982\u679c\u901a\u8fc7Function\u63a5\u53e3\u63a5\u53e3\u6d88\u706dif\u2026else\u2026\u5462\uff1f\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u63a2\u7d22\u4e00\u4e0b\u5427\u3002<\/p>\n<h3>2 Function\u63a5\u53e3<\/h3>\n<p>Function\u63a5\u53e3\u5c31\u662f\u4e00\u4e2a\u6709\u4e14\u4ec5\u6709\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\uff0c\u4f46\u662f\u53ef\u4ee5\u6709\u591a\u4e2a\u975e\u62bd\u8c61\u65b9\u6cd5\u7684\u63a5\u53e3,Function\u63a5\u53e3\u53ef\u4ee5\u88ab\u9690\u5f0f\u8f6c\u6362\u4e3a lambda \u8868\u8fbe\u5f0f\u3002\u53ef\u4ee5\u901a\u8fc7FunctionalInterface\u6ce8\u89e3\u6765\u6821\u9a8cFunction\u63a5\u53e3\u7684\u6b63\u786e\u6027\u3002Java 8\u5141\u8bb8\u5728\u63a5\u53e3\u4e2d\u52a0\u5165\u5177\u4f53\u65b9\u6cd5\u3002\u63a5\u53e3\u4e2d\u7684\u5177\u4f53\u65b9\u6cd5\u6709\u4e24\u79cd\uff0cdefault\u65b9\u6cd5\u548cstatic\u65b9\u6cd5\u3002<\/p>\n<pre><code>@FunctionalInterface\ninterface TestFunctionService\n{\n    void addHttp(String url);\n}<\/code><\/pre>\n<p>\u90a3\u4e48\u5c31\u53ef\u4ee5\u4f7f\u7528Lambda\u8868\u8fbe\u5f0f\u6765\u8868\u793a\u8be5\u63a5\u53e3\u7684\u4e00\u4e2a\u5b9e\u73b0\u3002<\/p>\n<pre><code>TestFunctionService testFunctionService = url -&gt; System.out.println(&quot;http:&quot; + url);<\/code><\/pre>\n<h4>2.1 FunctionalInterface<\/h4>\n<h5>2.1.1 \u6e90\u7801<\/h5>\n<pre><code>@Documented\n@Retention(RetentionPolicy.RUNTIME)\n@Target(ElementType.TYPE)\npublic @interface FunctionalInterface {}<\/code><\/pre>\n<h5>2.1.2 \u8bf4\u660e<\/h5>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042924202\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e0a\u56fe\u662fFunctionalInterface\u7684\u6ce8\u89e3\u8bf4\u660e\u3002\u901a\u8fc7\u4e0a\u9762\u7684\u6ce8\u89e3\u8bf4\u660e\uff0c\u53ef\u4ee5\u77e5\u9053FunctionalInterface\u662f\u4e00\u4e2a\u6ce8\u89e3\uff0c\u7528\u6765\u8bf4\u660e\u4e00\u4e2a\u63a5\u53e3\u662f\u51fd\u6570\u5f0f\u63a5\u53e3\u3002 \u51fd\u6570\u5f0f\u63a5\u53e3\u53ea\u6709\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u3002 \u53ef\u4ee5\u6709\u9ed8\u8ba4\u65b9\u6cd5\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u65b9\u6cd5\u6709\u4e00\u4e2a\u5b9e\u73b0\uff0c\u6240\u4ee5\u4e0d\u662f\u62bd\u8c61\u7684\u3002\u51fd\u6570\u63a5\u53e3\u7684\u5b9e\u4f8b\u53ef\u4ee5\u7528lambda\u8868\u8fbe\u5f0f\u3001\u65b9\u6cd5\u5f15\u7528\u6216\u6784\u9020\u51fd\u6570\u5f15\u7528\u521b\u5efa\u3002<\/p>\n<p>FunctionalInterface\u4f1a\u6821\u9a8c\u63a5\u53e3\u662f\u5426\u6ee1\u8db3\u51fd\u6570\u5f0f\u63a5\u53e3\uff1a<\/p>\n<ul>\n<li>\u7c7b\u578b\u5fc5\u987b\u662f\u63a5\u53e3\u7c7b\u578b\uff0c\u4e0d\u80fd\u662f\u6ce8\u91ca\u7c7b\u578b\u3001\u679a\u4e3e\u6216\u7c7b\u3002<\/li>\n<li>\u53ea\u80fd\u6709\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u3002<\/li>\n<li>\u53ef\u4ee5\u6709\u591a\u4e2a\u9ed8\u8ba4\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5\u3002<\/li>\n<li>\u53ef\u4ee5\u663e\u793a\u8986\u76d6java.lang.Object\u4e2d\u7684\u62bd\u8c61\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u7f16\u8bd1\u5668\u4f1a\u5c06\u6ee1\u8db3\u51fd\u6570\u5f0f\u63a5\u53e3\u5b9a\u4e49\u7684\u4efb\u4f55\u63a5\u53e3\u89c6\u4e3a\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u800c\u4e0d\u7ba1\u8be5\u63a5\u53e3\u58f0\u660e\u4e2d\u662f\u5426\u4f7f\u7528FunctionalInterface\u6ce8\u89e3\u3002<\/p>\n<h3>3 Function\u63a5\u53e3\u4e3b\u8981\u5206\u7c7b<\/h3>\n<p>Function\u63a5\u53e3\u4e3b\u8981\u5206\u7c7b:<\/p>\n<ul>\n<li>Function\uff1aFunction\u51fd\u6570\u7684\u8868\u73b0\u5f62\u5f0f\u4e3a\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002<\/li>\n<li>Supplier\uff1aSupplier\u7684\u8868\u73b0\u5f62\u5f0f\u4e3a\u4e0d\u63a5\u53d7\u53c2\u6570\u3001\u53ea\u8fd4\u56de\u6570\u636e\u3002<\/li>\n<li>Consumer\uff1aConsumer\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u6ca1\u6709\u8fd4\u56de\u503c\u3002<\/li>\n<li>Runnable\uff1aRunnable\u7684\u8868\u73b0\u5f62\u5f0f\u4e3a\u5373\u6ca1\u6709\u53c2\u6570\u4e5f\u6ca1\u6709\u8fd4\u56de\u503c\u3002<\/li>\n<\/ul>\n<h4>3.1 Function<\/h4>\n<p>Function\u51fd\u6570\u7684\u8868\u73b0\u5f62\u5f0f\u4e3a\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002<\/p>\n<h5>3.1.1 \u6e90\u7801<\/h5>\n<pre><code>@FunctionalInterface\npublic interface Function&lt;T, R&gt; {\n    R apply(T t);\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    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    static &lt;T&gt; Function&lt;T, T&gt; identity() {\n        return t -&gt; t;\n    }\n}<\/code><\/pre>\n<h5>3.1.2 \u65b9\u6cd5\u8bf4\u660e<\/h5>\n<ul>\n<li>apply\uff1a\u62bd\u8c61\u65b9\u6cd5\u3002\u5c06\u6b64\u51fd\u6570\u5e94\u7528\u4e8e\u7ed9\u5b9a\u7684\u53c2\u6570\u3002\u53c2\u6570t\u901a\u8fc7\u5177\u4f53\u7684\u5b9e\u73b0\u8fd4\u56deR\u3002<\/li>\n<li>compose\uff1adefault\u65b9\u6cd5\u3002\u8fd4\u56de\u4e00\u4e2a\u590d\u5408\u51fd\u6570\uff0c\u9996\u5148\u6267\u884cfefore\u51fd\u6570\u5e94\u7528\u4e8e\u8f93\u5165\uff0c\u7136\u540e\u5c06\u8be5\u51fd\u6570\u5e94\u7528\u4e8e\u7ed3\u679c\u3002\u5982\u679c\u4efb\u610f\u4e00\u4e2a\u51fd\u6570\u7684\u6c42\u503c\u5f15\u53d1\u5f02\u5e38\uff0c\u5219\u5c06\u5176\u4f20\u9012\u7ed9\u7ec4\u5408\u51fd\u6570\u7684\u8c03\u7528\u8005\u3002<\/li>\n<li>andThen\uff1adefault\u65b9\u6cd5\u3002\u8fd4\u56de\u4e00\u4e2a\u590d\u5408\u51fd\u6570\uff0c\u8be5\u590d\u5408\u51fd\u6570\u9996\u5148\u5bf9\u5176\u5e94\u7528\u6b64\u51fd\u6570\u5b83\u7684\u8f93\u5165\uff0c\u7136\u540e\u5bf9\u7ed3\u679c\u5e94\u7528after\u51fd\u6570\u3002\u5982\u679c\u4efb\u610f\u4e00\u4e2a\u51fd\u6570\u7684\u6c42\u503c\u5f15\u53d1\u5f02\u5e38\uff0c\u5219\u5c06\u5176\u4f20\u9012\u7ed9\u7ec4\u5408\u51fd\u6570\u7684\u8c03\u7528\u8005\u3002<\/li>\n<li>identity\uff1astatic\u65b9\u6cd5\u3002\u8fd4\u56de\u4e00\u4e2a\u59cb\u7ec8\u8fd4\u56de\u5176\u8f93\u5165\u53c2\u6570\u7684\u51fd\u6570\u3002<\/li>\n<\/ul>\n<h5>3.1.3 \u65b9\u6cd5\u4e3e\u4f8b<\/h5>\n<p>1\uff09apply<\/p>\n<p>\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre><code>public  String upString(String str){\n    Function&lt;String, String&gt; function1 = s -&gt; s.toUpperCase();\n    return function1.apply(str);\n}\n public static void main(String[] args) {\n     System.out.println(upString(&quot;hello!&quot;));\n }<\/code><\/pre>\n<p>\u901a\u8fc7apply\u8c03\u7528\u5177\u4f53\u7684\u5b9e\u73b0\u3002\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042924203\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>2\uff09compose<\/p>\n<p>\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre><code>public static void main(String[] args) {\n    Function&lt;String, String&gt; function1 = s -&gt; s.toUpperCase();\n    Function&lt;String, String&gt; function2 = s -&gt; &quot;my name is &quot;+s;\n    String result = function1.compose(function2).apply(&quot;zhangSan&quot;);\n    System.out.println(result);\n}<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042924204\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5982\u7ed3\u679c\u6240\u793a\uff1acompose \u5148\u6267\u884cfunction2 \u540e\u6267\u884cfunction1\u3002<\/p>\n<p>3\uff09andThen<\/p>\n<p>\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre><code>public static void main(String[] args) {\n    Function&lt;String, String&gt; function1 = s -&gt; s.toUpperCase();\n    Function&lt;String, String&gt; function2 = s -&gt; &quot;my name is &quot;+s;\n    String result = function1.andThen(function2).apply(&quot;zhangSan&quot;);\n    System.out.println(result);\n}<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042924205\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5982\u7ed3\u679c\u6240\u793a\uff1a<\/p>\n<p>andThen\u5148\u6267\u884cfunction1 \u540e\u6267\u884cfunction2\u3002<\/p>\n<ul>\n<li>identity<\/li>\n<\/ul>\n<p>\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre><code>public static void main(String[] args) {\n    Stream&lt;String&gt; stream = Stream.of(&quot;order&quot;, &quot;good&quot;, &quot;lab&quot;, &quot;warehouse&quot;);\n    Map&lt;String, Integer&gt; map = stream.collect(Collectors.toMap(Function.identity(), String::length));\n    System.out.println(map);\n}<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c:<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042924206\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h4>3.2 Supplier<\/h4>\n<p>Supplier\u7684\u8868\u73b0\u5f62\u5f0f\u4e3a\u4e0d\u63a5\u53d7\u53c2\u6570\u3001\u53ea\u8fd4\u56de\u6570\u636e\u3002<\/p>\n<h5>3.2.1 \u6e90\u7801<\/h5>\n<pre><code>@FunctionalInterface\npublic interface Supplier&lt;T&gt; {\n    \/**\n     * Gets a result.\n     *\n     * @return a result\n     *\/\n    T get();\n}<\/code><\/pre>\n<h5>3.2.2 \u65b9\u6cd5\u8bf4\u660e<\/h5>\n<p>get\uff1a\u62bd\u8c61\u65b9\u6cd5\u3002\u901a\u8fc7\u5b9e\u73b0\u8fd4\u56deT\u3002<\/p>\n<h5>3.2.3 \u65b9\u6cd5\u4e3e\u4f8b<\/h5>\n<pre><code>public class SupplierTest {\n    SupplierTest(){\n        System.out.println(Math.random());\n        System.out.println(this.toString());\n    }\n}\n    public static void main(String[] args) {\n        Supplier&lt;SupplierTest&gt; sup = SupplierTest::new;\n        System.out.println(&quot;\u8c03\u7528\u4e00\u6b21&quot;);\n        sup.get();\n        System.out.println(&quot;\u8c03\u7528\u4e8c\u6b21&quot;);\n        sup.get();\n}<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c:<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042924207\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5982\u7ed3\u679c\u6240\u793a\uff1aSupplier\u5efa\u7acb\u65f6\u5e76\u6ca1\u6709\u521b\u5efa\u65b0\u7c7b\uff0c\u6bcf\u6b21\u8c03\u7528get\u8fd4\u56de\u7684\u503c\u4e0d\u662f\u540c\u4e00\u4e2a\u3002<\/p>\n<h4>3.3 Consumer<\/h4>\n<p>Consumer\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u6ca1\u6709\u8fd4\u56de\u503c\u3002<\/p>\n<h5>3.3.1 \u6e90\u7801<\/h5>\n<pre><code>@FunctionalInterface\npublic interface Consumer&lt;T&gt; {\n    void accept(T t);\n    default Consumer&lt;T&gt; andThen(Consumer&lt;? super T&gt; after) {\n        Objects.requireNonNull(after);\n        return (T t) -&gt; { accept(t); after.accept(t); };\n    }\n}<\/code><\/pre>\n<h5>3.3.2 \u65b9\u6cd5\u8bf4\u660e<\/h5>\n<ul>\n<li>accept\uff1a\u5bf9\u7ed9\u5b9a\u53c2\u6570T\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\u3002<\/li>\n<li>andThen\uff1a\u6309\u987a\u5e8f\u6267\u884cConsumer -&gt; after ,\u5982\u679c\u6267\u884c\u64cd\u4f5c\u5f15\u53d1\u5f02\u5e38\uff0c\u8be5\u5f02\u5e38\u88ab\u4f20\u9012\u7ed9\u8c03\u7528\u8005\u3002<\/li>\n<\/ul>\n<h5>3.3.3 \u65b9\u6cd5\u4e3e\u4f8b<\/h5>\n<pre><code>public static void main(String[] args) {\n    Consumer&lt;String&gt; consumer = s -&gt; System.out.println(&quot;consumer_&quot;+s);\n    Consumer&lt;String&gt; after = s -&gt; System.out.println(&quot;after_&quot;+s);\n    consumer.accept(&quot;isReady&quot;);\n    System.out.println(&quot;========================&quot;);\n    consumer.andThen(after).accept(&quot;is coming&quot;);\n}<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042924208\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5982\u7ed3\u679c\u6240\u793a\uff1a\u5bf9\u540c\u4e00\u4e2a\u53c2\u6570T\uff0c\u901a\u8fc7andThen \u65b9\u6cd5\uff0c\u5148\u6267\u884cconsumer\uff0c\u518d\u6267\u884cfater\u3002<\/p>\n<h4>3.4 Runnable<\/h4>\n<p>Runnable\uff1aRunnable\u7684\u8868\u73b0\u5f62\u5f0f\u4e3a\u5373\u6ca1\u6709\u53c2\u6570\u4e5f\u6ca1\u6709\u8fd4\u56de\u503c\u3002<\/p>\n<h5>3.4.1 \u6e90\u7801<\/h5>\n<pre><code>@FunctionalInterface\npublic interface Runnable {\n    public abstract void run();\n}<\/code><\/pre>\n<h5>3.4.2 \u65b9\u6cd5\u8bf4\u660e<\/h5>\n<p>run\uff1a\u62bd\u8c61\u65b9\u6cd5\u3002run\u65b9\u6cd5\u5b9e\u73b0\u5177\u4f53\u7684\u5185\u5bb9\uff0c\u9700\u8981\u5c06Runnale\u653e\u5165\u5230Thread\u4e2d\uff0c\u901a\u8fc7Thread\u7c7b\u4e2d\u7684start()\u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b\uff0c\u6267\u884crun\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n<h5>3.4.3 \u65b9\u6cd5\u4e3e\u4f8b<\/h5>\n<pre><code>public class TestRun implements Runnable {\n    @Override\n    public void run() {\n        System.out.println(&quot;TestRun is running!&quot;);\n    }\n}\n    public static void main(String[] args) {\n        Thread thread = new Thread(new TestRun());\n        thread.start();\n    }<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042924209\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5982\u7ed3\u679c\u6240\u793a\uff1a\u5f53\u7ebf\u7a0b\u5b9e\u884cstart\u65b9\u6cd5\u65f6\uff0c\u6267\u884cRunnable \u7684run\u65b9\u6cd5\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n<h3>4 Function\u63a5\u53e3\u7528\u6cd5<\/h3>\n<p>Function\u7684\u4e3b\u8981\u7528\u9014\u662f\u53ef\u4ee5\u901a\u8fc7lambda \u8868\u8fbe\u5f0f\u5b9e\u73b0\u65b9\u6cd5\u7684\u5185\u5bb9\u3002<\/p>\n<h4>4.1 \u5dee\u5f02\u5904\u7406<\/h4>\n<p>\u539f\u4ee3\u7801\uff1a<\/p>\n<pre><code>@Data\npublic class User {\n    \/**\n     * \u59d3\u540d\n     *\/\n    private String name;\n    \/**\n     * \u5e74\u9f84\n     *\/\n    private int age;\n    \/**\n     * \u7ec4\u5458\n     *\/\n    private List&lt;User&gt; parters;\n}\n    public static void main(String[] args) {\n        User user =new User();\n        if(user ==null ||user.getAge() &lt;18 ){\n            throw new RuntimeException(&quot;\u672a\u6210\u5e74\uff01&quot;);\n        }\n}<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042924210\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4f7f\u7528Function\u63a5\u53e3\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code>@FunctionalInterface\npublic interface testFunctionInfe {\n    \/**\n     * \u8f93\u5165\u5f02\u5e38\u4fe1\u606f\n     * @param message\n     *\/\n    void showExceptionMessage(String message);\n}\n    public static testFunctionInfe doException(boolean flag){\n        return (message -&gt; {\n            if (flag){\n                throw new RuntimeException(message);\n            }\n        });\n    }\n    public static void main(String[] args) {\n        User user =new User();\n        doException(user ==null ||user.getAge() &lt;18).showExceptionMessage(&quot;\u672a\u6210\u5e74\uff01&quot;);\n}<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042924211\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4f7f\u7528function\u63a5\u53e3\u524d\u540e\u90fd\u629b\u51fa\u4e86\u6307\u5b9a\u7684\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<h4>4.2 \u5904\u7406if\u2026else\u2026<\/h4>\n<p>\u539f\u4ee3\u7801:<\/p>\n<pre><code>public static void main(String[] args) {\n    User user =new User();\n    if(user==null){\n        System.out.println(&quot;\u65b0\u589e\u7528\u6237&quot;);\n    }else {\n        System.out.println(&quot;\u66f4\u65b0\u7528\u6237&quot;);\n    }\n}<\/code><\/pre>\n<p>\u4f7f\u7528Function\u63a5\u53e3\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code>public static void main(String[] args) {\n    User user =new User();\n    Consumer trueConsumer = o -&gt; {\n        System.out.println(&quot;\u65b0\u589e\u7528\u6237&quot;);\n    };\n    Consumer falseConsumer= o -&gt; {\n        System.out.println(&quot;\u66f4\u65b0\u7528\u6237&quot;);\n    };\n    trueOrFalseMethdo(user).showExceptionMessage(trueConsumer,falseConsumer);\n}\npublic static testFunctionInfe trueOrFalseMethdo(User user){\n    return ((trueConsumer, falseConsumer) -&gt; {\n        if(user==null){\n            trueConsumer.accept(user);\n        }else {\n            falseConsumer.accept(user);\n        }\n    });\n}\n@FunctionalInterface\npublic interface testFunctionInfe {\n    \/**\n     * \u4e0d\u540c\u5206\u5904\u7406\u4e0d\u540c\u7684\u4e8b\u60c5\n     * @param trueConsumer\n     * @param falseConsumer\n     *\/\n    void showExceptionMessage(Consumer trueConsumer,Consumer falseConsumer);\n}<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042924212\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h4>4.3 \u5904\u7406\u591a\u4e2aif<\/h4>\n<p>\u539f\u4ee3\u7801\uff1a<\/p>\n<pre><code>public static void main(String[] args) {\n    String flag=&quot;&quot;;\n    if(&quot;A&quot;.equals(flag)){\n        System.out.println(&quot;\u6211\u662fA&quot;);\n    }else if (&quot;B&quot;.equals(flag)) {\n        System.out.println(&quot;\u6211\u662fB&quot;);\n    }else if (&quot;C&quot;.equals(flag)) {\n        System.out.println(&quot;\u6211\u662fC&quot;);\n    }else {\n        System.out.println(&quot;\u6ca1\u6709\u5bf9\u5e94\u7684\u6307\u4ee4&quot;);\n    }\n}<\/code><\/pre>\n<p>\u4f7f\u7528Function\u63a5\u53e3\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code>public static void main(String[] args) {\n    String flag=&quot;B&quot;;\n    Map&lt;String, Runnable&gt; map =initFunctionMap();\n    trueOrFalseMethdo(map.get(flag)==null).showExceptionMessage(()-&gt;{\n        System.out.println(&quot;\u6ca1\u6709\u76f8\u5e94\u6307\u4ee4&quot;);\n    },map.get(flag));\n}\npublic static   Map&lt;String, Runnable&gt; initFunctionMap(){\n    Map&lt;String,Runnable&gt; result  = Maps.newHashMap();\n    result.put(&quot;A&quot;,()-&gt;{System.out.println(&quot;\u6211\u662fA&quot;);});\n    result.put(&quot;B&quot;,()-&gt;{System.out.println(&quot;\u6211\u662fB&quot;);});\n    result.put(&quot;C&quot;,()-&gt;{System.out.println(&quot;\u6211\u662fC&quot;);});\n    return result;\n}\npublic static testFunctionInfe trueOrFalseMethdo(boolean flag){\n    return ((runnable, falseConsumer) -&gt; {\n        if(flag){\n            runnable.run();\n        }else {\n            falseConsumer.run();\n        }\n    });\n}<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c:<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042924213\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>5 \u603b\u7ed3<\/h3>\n<p>Function\u51fd\u6570\u5f0f\u63a5\u53e3\u662fjava 8\u65b0\u52a0\u5165\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u548clambda\u8868\u8fbe\u5f0f\u5b8c\u7f8e\u7ed3\u5408\uff0c\u662f\u975e\u5e38\u91cd\u8981\u7684\u7279\u6027,\u53ef\u4ee5\u6781\u5927\u7684\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042924194\">https:\/\/segmentfault.com\/a\/1190000042924194<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u9648\u660c\u6d69 1 \u5bfc\u8bfb if\u2026else\u2026\u5728\u4ee3\u7801\u4e2d\u7ecf\u5e38\u4f7f\u7528\uff0c\u542c\u8bf4\u53ef\u4ee5\u901a\u8fc7Java 8\u7684Function\u63a5\u53e3\u6765\u6d88\u706d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006933","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006933"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006933\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}