{"id":12564,"date":"2023-09-05T17:13:55","date_gmt":"2023-09-05T09:13:55","guid":{"rendered":"https:\/\/92it.top\/?p=12564"},"modified":"2023-09-05T17:29:38","modified_gmt":"2023-09-05T09:29:38","slug":"java8%e4%b9%8bconsumer%e3%80%81supplier","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=12564","title":{"rendered":"Java8\u4e4bConsumer\u3001Supplier"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/76340256\">https:\/\/zhuanlan.zhihu.com\/p\/76340256<\/a><\/p>\n\n\n\n<p>\u4eca\u5929\u6211\u4eec\u8fd8\u8bb2\u8bb2<strong>Consumer\u3001Supplier\u3001Predicate\u3001Function<\/strong>\u8fd9\u51e0\u4e2a\u63a5\u53e3\u7684\u7528\u6cd5\uff0c\u5728 Java8 \u7684\u7528\u6cd5\u5f53\u4e2d\uff0c\u8fd9\u51e0\u4e2a\u63a5\u53e3\u867d\u7136\u6ca1\u6709\u660e\u76ee\u5f20\u80c6\u7684\u4f7f\u7528\uff0c\u4f46\u662f\uff0c\u5374\u662f\u6da6\u7269\u7ec6\u65e0\u58f0\u7684\u3002\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u8bf4\u5462\uff1f<\/p>\n\n\n\n<p>\u8fd9\u51e0\u4e2a\u63a5\u53e3\u90fd\u5728 <code>java.util.function<\/code> \u5305\u4e0b\u7684\uff0c\u5206\u522b\u662fConsumer\uff08\u6d88\u8d39\u578b\uff09\u3001supplier\uff08\u4f9b\u7ed9\u578b\uff09\u3001predicate\uff08\u8c13\u8bcd\u578b\uff09\u3001function\uff08\u529f\u80fd\u6027\uff09\uff0c\u76f8\u4fe1\u6709\u4e86\u540e\u9762\u7684\u89e3\u91ca\uff0c\u4f60\u5e94\u8be5\u975e\u5e38\u6e05\u695a\u8fd9\u4e2a\u63a5\u53e3\u7684\u529f\u80fd\u4e86\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\uff0c\u4e0b\u9762\uff0c\u6211\u4eec\u4ece\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u6765\u8bb2\u8bb2\u8fd9\u4e2a\u63a5\u53e3\u7684\u7528\u6cd5\uff01<\/p>\n\n\n\n<p><strong>1 Consumer\u63a5\u53e3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4ece\u5b57\u9762\u610f\u601d\u4e0a\u6211\u4eec\u5c31\u53ef\u4ee5\u770b\u5f97\u51fa\u5566\uff0c<code>consumer\u63a5\u53e3<\/code>\u5c31\u662f\u4e00\u4e2a\u6d88\u8d39\u578b\u7684\u63a5\u53e3\uff0c\u901a\u8fc7\u4f20\u5165\u53c2\u6570\uff0c\u7136\u540e\u8f93\u51fa\u503c\uff0c\u5c31\u662f\u8fd9\u4e48\u7b80\u5355\uff0cJava8 \u7684\u4e00\u4e9b\u65b9\u6cd5\u770b\u8d77\u6765\u5f88\u62bd\u8c61\uff0c\u5176\u5b9e\uff0c\u53ea\u8981\u4f60\u7406\u89e3\u4e86\u5c31\u89c9\u5f97\u5f88\u597d\u7528\uff0c\u5e76\u4e14\u975e\u5e38\u7684\u7b80\u5355\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4e0b\u9762\u5c31\u5148\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u7136\u540e\u518d\u6765\u5206\u6790\u8fd9\u4e2a\u63a5\u53e3\u3002<\/p>\n\n\n\n<p><strong>1.1 Consumer\u5b9e\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/**\n     * consumer\u63a5\u53e3\u6d4b\u8bd5\n     *\/\n    @Test\n    public void test_Consumer() {\n        \/\/\u2460 \u4f7f\u7528consumer\u63a5\u53e3\u5b9e\u73b0\u65b9\u6cd5\n        Consumer&lt;String> consumer = new Consumer&lt;String>() {\n\n            @Override\n            public void accept(String s) {\n                System.out.println(s);\n            }\n        };\n        Stream&lt;String> stream = Stream.of(\"aaa\", \"bbb\", \"ddd\", \"ccc\", \"fff\");\n        stream.forEach(consumer);\n\n        System.out.println(\"********************\");\n\n        \/\/\u2461 \u4f7f\u7528lambda\u8868\u8fbe\u5f0f\uff0cforEach\u65b9\u6cd5\u9700\u8981\u7684\u5c31\u662f\u4e00\u4e2aConsumer\u63a5\u53e3\n        stream = Stream.of(\"aaa\", \"bbb\", \"ddd\", \"ccc\", \"fff\");\n        Consumer&lt;String> consumer1 = (s) -> System.out.println(s);\/\/lambda\u8868\u8fbe\u5f0f\u8fd4\u56de\u7684\u5c31\u662f\u4e00\u4e2aConsumer\u63a5\u53e3\n        stream.forEach(consumer1);\n        \/\/\u66f4\u76f4\u63a5\u7684\u65b9\u5f0f\n        \/\/stream.forEach((s) -> System.out.println(s));\n        System.out.println(\"********************\");\n\n        \/\/\u2462 \u4f7f\u7528\u65b9\u6cd5\u5f15\u7528\uff0c\u65b9\u6cd5\u5f15\u7528\u4e5f\u662f\u4e00\u4e2aconsumer\n        stream = Stream.of(\"aaa\", \"bbb\", \"ddd\", \"ccc\", \"fff\");\n        Consumer consumer2 = System.out::println;\n        stream.forEach(consumer);\n        \/\/\u66f4\u76f4\u63a5\u7684\u65b9\u5f0f\n        \/\/stream.forEach(System.out::println);\n    }<\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-17-770x1024.png\" alt=\"\" class=\"wp-image-12565\" width=\"236\" height=\"314\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-17-770x1024.png 770w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-17-226x300.png 226w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-17-768x1021.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-17-830x1104.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-17-230x306.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-17-350x466.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-17-480x638.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-17.png 1018w\" sizes=\"(max-width: 236px) 100vw, 236px\" \/><\/figure><\/div>\n\n\n\n<p><strong>1.2 \u5b9e\u4f8b\u5206\u6790<\/strong><\/p>\n\n\n\n<p><strong>\u2460 <code>consumer<\/code>\u63a5\u53e3\u5206\u6790<\/strong><\/p>\n\n\n\n<p>\u5728\u4ee3\u7801\u2460\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u521b\u5efa <code>Consumer<\/code> \u63a5\u53e3\uff0c\u5e76\u4e14\u5b9e\u73b0\u4e86\u4e00\u4e2a\u540d\u4e3a <code>accept<\/code> \u7684\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u662f\u8fd9\u4e2a\u63a5\u53e3\u7684\u5173\u952e\u4e86\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u770b\u4e00\u4e0b <code>accept<\/code> \u65b9\u6cd5\uff1b\u8fd9\u4e2a\u65b9\u6cd5\u4f20\u5165\u4e00\u4e2a\u53c2\u6570\uff0c\u4e0d\u8fd4\u56de\u503c\u3002\u5f53\u6211\u4eec\u53d1\u73b0 <code>forEach<\/code> \u9700\u8981\u4e00\u4e2a <code>Consumer<\/code> \u7c7b\u578b\u7684\u53c2\u6570\u7684\u65f6\u5019\uff0c\u4f20\u5165\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u8f93\u51fa\u5bf9\u5e94\u7684\u503c\u4e86\u3002<\/p>\n\n\n\n<p><strong>\u2461 lambda \u8868\u8fbe\u5f0f\u4f5c\u4e3a consumer<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Consumer&lt;String> consumer1 = (s) -> System.out.println(s);\/\/lambda\u8868\u8fbe\u5f0f\u8fd4\u56de\u7684\u5c31\u662f\u4e00\u4e2aConsumer\u63a5\u53e3<\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e0b\u9762\u7684 <code>lambda<\/code> \u8868\u8fbe\u5f0f\u4f5c\u4e3a <code>Consumer<\/code>\u3002\u4ed4\u7ec6\u7684\u770b\u4e00\u4e0b\u4f60\u4f1a\u53d1\u73b0\uff0c<code>lambda<\/code> \u8868\u8fbe\u5f0f\u8fd4\u56de\u503c\u5c31\u662f\u4e00\u4e2a <code>Consumer<\/code>\uff1b\u6240\u4ee5\uff0c\u4f60\u4e5f\u5c31\u80fd\u591f\u7406\u89e3\u4e3a\u4ec0\u4e48 <code>forEach<\/code> \u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528 lamdda \u8868\u8fbe\u5f0f\u4f5c\u4e3a\u53c2\u6570\u4e86\u5427\u3002<\/p>\n\n\n\n<p><strong>\u2462 \u65b9\u6cd5\u5f15\u7528\u4f5c\u4e3a consumer<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Consumer consumer2 = System.out::println;<\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7528\u4e86\u4e00\u4e2a<strong>\u65b9\u6cd5\u5f15\u7528<\/strong>\u7684\u65b9\u5f0f\u4f5c\u4e3a\u4e00\u4e2a Consumer \uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u4f20\u7ed9 <code>forEach<\/code> \u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><strong>1.3 \u5176\u4ed6 Consumer \u63a5\u53e3<\/strong><\/p>\n\n\n\n<p>\u9664\u4e86\u4e0a\u9762\u4f7f\u7528\u7684 Consumer \u63a5\u53e3\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u8fd9\u4e9b Consumer \u63a5\u53e3\u3002<br><code>IntConsumer\u3001DoubleConsumer\u3001LongConsumer\u3001BiConsumer<\/code>\uff0c\u4f7f\u7528\u65b9\u6cd5\u548c\u4e0a\u9762\u4e00\u6837\u3002<\/p>\n\n\n\n<p><strong>1.4 Consumer \u603b\u7ed3<\/strong><\/p>\n\n\n\n<p>\u770b\u5b8c\u4e0a\u9762\u7684\u5b9e\u4f8b\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u4e3a\u51e0\u70b9\u3002<\/p>\n\n\n\n<p>\u2460 Consumer\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u5e76\u4e14\u53ea\u8981\u5b9e\u73b0\u4e00\u4e2a <code>accept<\/code> \u65b9\u6cd5\uff0c\u5c31\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a<strong>\u201c\u6d88\u8d39\u8005\u201d<\/strong>\u8f93\u51fa\u4fe1\u606f\u3002<br>\u2461 \u5176\u5b9e\uff0clambda \u8868\u8fbe\u5f0f\u3001\u65b9\u6cd5\u5f15\u7528\u7684\u8fd4\u56de\u503c\u90fd\u662f <strong>Consumer \u7c7b\u578b<\/strong>\uff0c\u6240\u4ee5\uff0c\u4ed6\u4eec\u80fd\u591f\u4f5c\u4e3a <code>forEach<\/code> \u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u5e76\u4e14\u8f93\u51fa\u4e00\u4e2a\u503c\u3002<\/p>\n\n\n\n<p><strong>2 Supplier \u63a5\u53e3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Supplier \u63a5\u53e3\u662f\u4e00\u4e2a<strong>\u4f9b\u7ed9\u578b<\/strong>\u7684\u63a5\u53e3\uff0c\u5176\u5b9e\uff0c\u8bf4\u767d\u4e86\u5c31\u662f\u4e00\u4e2a<strong>\u5bb9\u5668<\/strong>\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u6570\u636e\uff0c\u7136\u540e\u53ef\u4ee5\u4f9b\u5176\u4ed6\u65b9\u6cd5\u4f7f\u7528\u7684\u8fd9\u4e48\u4e00\u4e2a\u63a5\u53e3\uff0c\u662f\u4e0d\u662f\u5f88\u660e\u767d\u4e86\uff0c\u5982\u679c\u8fd8\u662f\u4e0d\u660e\u767d\uff0c\u770b\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff0c\u4e00\u5b9a\u5f7b\u5e95\u641e\u61c2\uff01<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/pythonjishu.com\/tag\/java\/\" target=\"_blank\">Java<\/a> 8\u4e2d\u5f15\u5165\u7684Supplier\u8868\u793a\u4e00\u4e2a\u4f9b\u5e94\u5546\uff0c\u4ee3\u8868\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u4e0d\u9700\u8981\u4efb\u4f55\u8f93\u5165\u53c2\u6570\uff0c\u53ea\u8fd4\u56de\u4e00\u4e2a\u6211\u4eec\u5b9a\u4e49\u597d\u7684\u6570\u636e\u7c7b\u578b\u7684\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u8be5\u63a5\u53e3\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570\u5f0f\u65b9\u6cd5\uff0c\u5373get()\u65b9\u6cd5\uff0c\u7528\u4e8e\u83b7\u53d6\u8f93\u51fa\u7ed3\u679c\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@FunctionalInterface\npublic interface Supplier&lt;T> {\n\n    T get();\n}<\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0c<code>@FunctionalInterface<\/code>\u6ce8\u89e3\u8868\u793a\u8be5\u63a5\u53e3\u662f\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u5373\u8be5\u63a5\u53e3\u53ea\u80fd\u5b9a\u4e49\u4e00\u4e2a\u62bd\u8c61\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><strong>2.1 Supplier\u5b9e\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">**\n     * Supplier\u63a5\u53e3\u6d4b\u8bd5\uff0csupplier\u76f8\u5f53\u4e00\u4e2a\u5bb9\u5668\u6216\u8005\u53d8\u91cf\uff0c\u53ef\u4ee5\u5b58\u50a8\u503c\n     *\/\n    @Test\n    public void test_Supplier() {\n        \/\/\u2460 \u4f7f\u7528Supplier\u63a5\u53e3\u5b9e\u73b0\u65b9\u6cd5,\u53ea\u6709\u4e00\u4e2aget\u65b9\u6cd5\uff0c\u65e0\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u503c\n        Supplier&lt;Integer> supplier = new Supplier&lt;Integer>() {\n            @Override\n            public Integer get() {\n                \/\/\u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u503c\n                return new Random().nextInt();\n            }\n        };\n\n        System.out.println(supplier.get());\n\n        System.out.println(\"********************\");\n\n        \/\/\u2461 \u4f7f\u7528lambda\u8868\u8fbe\u5f0f\uff0c\n        supplier = () -> new Random().nextInt();\n        System.out.println(supplier.get());\n        System.out.println(\"********************\");\n\n        \/\/\u2462 \u4f7f\u7528\u65b9\u6cd5\u5f15\u7528\n        Supplier&lt;Double> supplier2 = Math::random;\n        System.out.println(supplier2.get());<\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-18.png\" alt=\"\" class=\"wp-image-12566\" width=\"196\" height=\"136\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-18.png 810w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-18-300x208.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-18-768x533.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-18-230x160.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-18-350x243.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-18-480x333.png 480w\" sizes=\"(max-width: 196px) 100vw, 196px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2.2 \u5b9e\u4f8b\u5206\u6790<\/strong><\/p>\n\n\n\n<p><strong>\u2460 Supplier\u63a5\u53e3\u5206\u6790<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Supplier&lt;Integer> supplier = new Supplier&lt;Integer>() {\n            @Override\n            public Integer get() {\n                \/\/\u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u503c\n                return new Random().nextInt();\n            }\n        };\n<\/pre>\n\n\n\n<p>\u770b\u4e00\u4e0b\u8fd9\u6bb5\u4ee3\u7801\uff0c\u6211\u4eec\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a Supplier \u5bf9\u8c61\uff0c\u5b9e\u73b0\u4e86\u4e00\u4e2a <code>get<\/code> \u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u65e0\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u503c\uff1b\u6240\u4ee5\uff0c\u6bcf\u6b21\u4f7f\u7528\u8fd9\u4e2a\u63a5\u53e3\u7684\u65f6\u5019\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u5e76\u4e14\u4fdd\u5b58\u5728\u8fd9\u4e2a\u63a5\u53e3\u4e2d\uff0c\u6240\u4ee5\u8bf4\u662f\u4e00\u4e2a<strong>\u5bb9\u5668<\/strong>\u3002<\/p>\n\n\n\n<p><strong>\u2461 lambda\u8868\u8fbe\u5f0f\u4f5c\u4e3a Supplier<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\u2461 \u4f7f\u7528lambda\u8868\u8fbe\u5f0f\uff0c\n        supplier = () -> new Random().nextInt();\n        System.out.println(supplier.get());\n        System.out.println(\"********************\");<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u8fd9\u6bb5\u4ee3\u7801\uff0c\u6211\u4eec\u4f7f\u7528 <strong>lambda \u8868\u8fbe\u5f0f<\/strong>\u8fd4\u56de\u4e00\u4e2a Supplier\u7c7b\u578b\u7684\u63a5\u53e3\uff0c\u7136\u540e\uff0c\u6211\u4eec\u8c03\u7528 <code>get<\/code> \u65b9\u6cd5\u5c31\u53ef\u4ee5\u83b7\u53d6\u8fd9\u4e2a\u503c\u4e86\u3002<\/p>\n\n\n\n<p><strong>\u2462 \u65b9\u6cd5\u5f15\u7528\u4f5c\u4e3a Supplier<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\u2462 \u4f7f\u7528\u65b9\u6cd5\u5f15\u7528\n        Supplier&lt;Double> supplier2 = Math::random;\n        System.out.println(supplier2.get());\n<\/pre>\n\n\n\n<p>\u65b9\u6cd5\u5f15\u7528\u4e5f\u662f\u8fd4\u56de\u4e00\u4e2aSupplier\u7c7b\u578b\u7684\u63a5\u53e3\u3002<\/p>\n\n\n\n<p><strong>2.3 Supplier \u5b9e\u4f8b2<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u770b\u5b8c\u7b2c\u4e00\u4e2a\u5b9e\u4f8b\u4e4b\u540e\uff0c\u6211\u4eec\u5e94\u8be5\u6709\u4e00\u4e2a\u4e86\u89e3\u4e86\uff0c\u4e0b\u9762\u518d\u770b\u4e00\u4e2a\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/**\n     * Supplier\u63a5\u53e3\u6d4b\u8bd52\uff0c\u4f7f\u7528\u9700\u8981Supplier\u7684\u63a5\u53e3\u65b9\u6cd5\n     *\/\n    @Test\n    public void test_Supplier2() {\n        Stream&lt;Integer> stream = Stream.of(1, 2, 3, 4, 5);\n        \/\/\u8fd4\u56de\u4e00\u4e2aoptional\u5bf9\u8c61\n        Optional&lt;Integer> first = stream.filter(i -> i > 4)\n                .findFirst();\n\n        \/\/optional\u5bf9\u8c61\u6709\u9700\u8981Supplier\u63a5\u53e3\u7684\u65b9\u6cd5\n        \/\/orElse\uff0c\u5982\u679cfirst\u4e2d\u5b58\u5728\u6570\uff0c\u5c31\u8fd4\u56de\u8fd9\u4e2a\u6570\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5c31\u653e\u56de\u4f20\u5165\u7684\u6570\n        System.out.println(first.orElse(1));\n        System.out.println(first.orElse(7));\n\n        System.out.println(\"********************\");\n\n        Supplier&lt;Integer> supplier = new Supplier&lt;Integer>() {\n            @Override\n            public Integer get() {\n                \/\/\u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u503c\n                return new Random().nextInt();\n            }\n        };\n\n        \/\/orElseGet\uff0c\u5982\u679cfirst\u4e2d\u5b58\u5728\u6570\uff0c\u5c31\u8fd4\u56de\u8fd9\u4e2a\u6570\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5c31\u8fd4\u56desupplier\u8fd4\u56de\u7684\u503c\n        System.out.println(first.orElseGet(supplier));\n    }<\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-19.png\" alt=\"\" class=\"wp-image-12567\" width=\"228\" height=\"121\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-19.png 734w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-19-300x159.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-19-230x122.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-19-350x186.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-19-480x255.png 480w\" sizes=\"(max-width: 228px) 100vw, 228px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u4ee3\u7801\u5206\u6790<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Optional&lt;Integer> first = stream.filter(i -> i > 4)\n                .findFirst();<\/pre>\n\n\n\n<p>\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u83b7\u53d6\u5230\u4e00\u4e2a Optional \u5bf9\u8c61\uff0c\u7136\u540e\uff0c\u5728 Optional \u5bf9\u8c61\u4e2d\u6709 orElse \u65b9\u6cd5 \u548c orElseGet \u662f\u9700\u8981\u4e00\u4e2a Supplier \u63a5\u53e3\u7684\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/optional\u5bf9\u8c61\u6709\u9700\u8981Supplier\u63a5\u53e3\u7684\u65b9\u6cd5\n        \/\/orElse\uff0c\u5982\u679cfirst\u4e2d\u5b58\u5728\u6570\uff0c\u5c31\u8fd4\u56de\u8fd9\u4e2a\u6570\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5c31\u653e\u56de\u4f20\u5165\u7684\u6570\n        System.out.println(first.orElse(1));\n        System.out.println(first.orElse(7));\n\n        System.out.println(\"********************\");\n\n        Supplier&lt;Integer> supplier = new Supplier&lt;Integer>() {\n            @Override\n            public Integer get() {\n                \/\/\u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u503c\n                return new Random().nextInt();\n            }\n        };\n\n        \/\/orElseGet\uff0c\u5982\u679cfirst\u4e2d\u5b58\u5728\u6570\uff0c\u5c31\u8fd4\u56de\u8fd9\u4e2a\u6570\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5c31\u8fd4\u56desupplier\u8fd4\u56de\u7684\u503c\n        System.out.println(first.orElseGet(supplier));<\/pre>\n\n\n\n<ul><li>orElse\uff1a\u5982\u679cfirst\u4e2d\u5b58\u5728\u6570\uff0c\u5c31\u8fd4\u56de\u8fd9\u4e2a\u6570\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5c31\u653e\u56de\u4f20\u5165\u7684\u6570<\/li><li>orElseGet\uff1a\u5982\u679cfirst\u4e2d\u5b58\u5728\u6570\uff0c\u5c31\u8fd4\u56de\u8fd9\u4e2a\u6570\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5c31\u8fd4\u56desupplier\u8fd4\u56de\u7684\u503c<\/li><\/ul>\n\n\n\n<p><strong>Supplier\u7684\u5e38\u89c1\u7528\u6cd5<\/strong><\/p>\n\n\n\n<p>\u5728Java 8\u4e2d\uff0cSupplier\u6bd4\u8f83\u5e38\u7528\u7684\u7528\u5904\u6709\u4e24\u4e2a\uff1a<\/p>\n\n\n\n<p><strong>1. \u5ef6\u8fdf\u8ba1\u7b97<\/strong><\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Supplier\u6765\u5ef6\u8fdf\u8ba1\u7b97\uff0c\u53ea\u6709\u5728\u8ba1\u7b97\u7ed3\u679c\u88ab\u771f\u6b63\u9700\u8981\u65f6\u624d\u4f1a\u88ab\u8ba1\u7b97\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Supplier&lt;Integer> integerSupplier = () -> {\n            System.out.println(\"\u6b63\u5728\u8ba1\u7b97...\");\n            return 10;\n        };\n\n        System.out.println(\"\u5f00\u59cb\u8fd0\u884c...\");\n        System.out.println(integerSupplier.get());<\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-20-1024x152.png\" alt=\"\" class=\"wp-image-12568\" width=\"477\" height=\"70\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-20-1024x152.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-20-300x45.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-20-768x114.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-20-1536x228.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-20-830x123.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-20-230x34.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-20-350x52.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-20-480x71.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-20.png 1654w\" sizes=\"(max-width: 477px) 100vw, 477px\" \/><\/figure><\/div>\n\n\n\n<p>\u53ef\u89c1\uff0c\u53ea\u6709\u5728<code>integerSupplier.get()<\/code>\u65b9\u6cd5\u8c03\u7528\u65f6\uff0c\u8ba1\u7b97\u7ed3\u679c\u624d\u771f\u6b63\u88ab\u8ba1\u7b97\u3002<\/p>\n\n\n\n<p><strong>2. \u7528\u4e8e\u6a21\u62df\u751f\u4ea7\u6570\u636e<\/strong><\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Supplier\u6765\u751f\u621010\u4e2a\u968f\u673a\u6570\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Supplier&lt;Integer> integerSupplier = () -> {\n            Random random = new Random();\n            return random.nextInt(100);\n        };\n\n        for (int i = 0; i &lt; 10; i++) {\n            System.out.println(integerSupplier.get());\n        }<\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-21-1024x313.png\" alt=\"\" class=\"wp-image-12569\" width=\"492\" height=\"150\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-21-1024x313.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-21-300x92.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-21-768x235.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-21-1536x470.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-21-830x254.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-21-230x70.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-21-350x107.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-21-480x147.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/\u56fe\u7247-21.png 1666w\" sizes=\"(max-width: 492px) 100vw, 492px\" \/><\/figure><\/div>\n\n\n\n<p>\u53ef\u89c1\uff0c\u901a\u8fc7Supplier\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u751f\u6210\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1ahttps:\/\/zhuanlan.zhihu.com\/p\/76340256 \u4eca\u5929\u6211\u4eec\u8fd8\u8bb2\u8bb2Consume [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12564"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12564"}],"version-history":[{"count":4,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12564\/revisions"}],"predecessor-version":[{"id":12573,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12564\/revisions\/12573"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}