{"id":1551,"date":"2026-06-05T15:38:43","date_gmt":"2026-06-05T07:38:43","guid":{"rendered":"https:\/\/yunyanglib.cn\/?p=1551"},"modified":"2026-06-05T15:38:45","modified_gmt":"2026-06-05T07:38:45","slug":"%e3%80%90java%e3%80%91stream-api-%e5%85%a8%e9%9d%a2%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/yunyanglib.cn\/?p=1551","title":{"rendered":"\u3010Java\u3011Stream API \u5168\u9762\u8be6\u89e3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u7ee7\u7eed\u5206\u4eab Java \u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u672c\u7bc7\u6765\u8be6\u7ec6\u8bb2\u89e3 <strong>Stream API<\/strong>\u3002Stream \u662f Java 8 \u5f15\u5165\u7684\u6838\u5fc3\u7279\u6027\u4e4b\u4e00\uff0c\u501f\u52a9\u5b83\u6211\u4eec\u53ef\u4ee5\u7528\u66f4\u7b80\u6d01\u3001\u4f18\u96c5\u7684\u65b9\u5f0f\u5904\u7406\u96c6\u5408\u3001\u6570\u7ec4\u7b49\u6570\u636e\u6e90\uff0c\u544a\u522b\u7e41\u7410\u7684\u5faa\u73af\u904d\u5386\uff0c\u8ba9\u4ee3\u7801\u53ef\u8bfb\u6027\u5927\u5e45\u63d0\u5347\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4ec0\u4e48\u662f Stream<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Stream \u53ef\u4ee5\u7406\u89e3\u4e3a<strong>\u9ad8\u7ea7\u8fed\u4ee3\u5668<\/strong>\uff0c\u5b83\u6709\u51e0\u4e2a\u6838\u5fc3\u7279\u70b9\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6d41<strong>\u53ea\u80fd\u904d\u5386\u4e00\u6b21<\/strong>\uff0c\u904d\u5386\u7ed3\u675f\u540e\u6d41\u5c31\u4f1a\u88ab\u9500\u6bc1\uff0c\u65e0\u6cd5\u91cd\u590d\u4f7f\u7528\uff1b<\/li>\n\n\n\n<li>\u64cd\u4f5c\u6d41\u5fc5\u987b\u4f9d\u6258\u6570\u636e\u6e90\uff0c\u5e38\u89c1\u6570\u636e\u6e90\u4e3a\u6570\u7ec4\u3001\u96c6\u5408\uff1b<\/li>\n\n\n\n<li>Stream \u652f\u6301\u94fe\u5f0f\u8c03\u7528\uff0c\u6bcf\u4e00\u6b21\u4e2d\u95f4\u64cd\u4f5c\u90fd\u4f1a\u8fd4\u56de\u65b0\u7684\u6d41\uff0c<strong>\u539f\u6709\u6d41\u5bf9\u8c61\u4e0d\u4f1a\u88ab\u6539\u53d8<\/strong>\uff1b<\/li>\n\n\n\n<li>\u6d41\u7684\u64cd\u4f5c\u5206\u4e3a\u4e24\u5927\u7c7b\uff1a<strong>\u4e2d\u95f4\u64cd\u4f5c<\/strong>\u548c<strong>\u7ec8\u7aef\u64cd\u4f5c<\/strong>\u3002<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e2d\u95f4\u64cd\u4f5c<\/strong>\uff1a\u53ef\u4ee5\u8fde\u7eed\u591a\u4e2a\uff0c\u4f5c\u7528\u662f\u5bf9\u6570\u636e\u505a\u8fc7\u6ee4\u3001\u8f6c\u6362\u7b49\u5904\u7406\uff0c\u6267\u884c\u540e\u4ecd\u8fd4\u56de\u6d41\uff0c\u652f\u6301\u94fe\u5f0f\u7f16\u7a0b\uff1b<\/li>\n\n\n\n<li><strong>\u7ec8\u7aef\u64cd\u4f5c<\/strong>\uff1a\u53ea\u80fd\u6709\u4e00\u4e2a\uff0c\u5fc5\u987b\u653e\u5728\u6240\u6709\u64cd\u4f5c\u7684\u672b\u5c3e\uff0c\u6267\u884c\u540e\u6d41\u7acb\u5373\u5173\u95ed\uff0c\u4e0d\u518d\u7ee7\u7eed\u6d41\u8f6c\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u521b\u5efa Stream \u6d41<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0c\u4e3b\u8981\u4ece<strong>\u6570\u7ec4<\/strong>\u548c<strong>\u96c6\u5408<\/strong>\u4e24\u7c7b\u6570\u636e\u6e90\u521b\u5efa\u6d41\uff0c\u4e0b\u9762\u5206\u573a\u666f\u6f14\u793a\u4ee3\u7801\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4ece\u6570\u7ec4\u521b\u5efa\u6d41<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u4f7f\u7528 <code>Arrays.stream()<\/code> \u6216 <code>Stream.of()<\/code> \u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Arrays;\nimport java.util.stream.Stream;\n\npublic class StreamDemo {\n    public static void main(String&#91;] args) {\n        \/\/ \u65b9\u5f0f1\uff1aArrays.stream() \u521b\u5efa\u6d41\n        Integer&#91;] arr = {1, 2, 3, 4, 5};\n        Stream&lt;Integer&gt; stream1 = Arrays.stream(arr);\n\n        \/\/ \u65b9\u5f0f2\uff1aStream.of() \u521b\u5efa\u6d41\n        Stream&lt;Integer&gt; stream2 = Stream.of(arr);\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4ece\u96c6\u5408\u521b\u5efa\u6d41<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Java 8 \u7ed9 <code>Collection<\/code> \u63a5\u53e3\u65b0\u589e\u4e86 <code>stream()<\/code> \u65b9\u6cd5\uff0c\u6240\u6709\u96c6\u5408\u90fd\u53ef\u76f4\u63a5\u8c03\u7528\uff1b\u540c\u65f6\u8fd8\u652f\u6301\u521b\u5efa<strong>\u5e76\u53d1\u6d41<\/strong><code>parallelStream()<\/code>\uff0c\u5e95\u5c42\u9ed8\u8ba4\u4f7f\u7528 <code>ForkJoinPool.commonPool()<\/code> \u7ebf\u7a0b\u6c60\uff0c\u9002\u5408\u5927\u6570\u636e\u91cf\u5e76\u884c\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\nimport java.util.List;\nimport java.util.stream.Stream;\n\npublic class StreamDemo {\n    public static void main(String&#91;] args) {\n        List&lt;String&gt; list = new ArrayList&lt;&gt;();\n        list.add(\"Java\");\n        list.add(\"MySQL\");\n        list.add(\"Linux\");\n\n        \/\/ \u666e\u901a\u4e32\u884c\u6d41\n        Stream&lt;String&gt; stream = list.stream();\n        \/\/ \u5e76\u53d1\u6d41\n        Stream&lt;String&gt; parallelStream = list.parallelStream();\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5e38\u7528\u6d41\u64cd\u4f5c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u5408\u4e2d\u95f4\u64cd\u4f5c\u3001\u7ec8\u7aef\u64cd\u4f5c\uff0c\u8bb2\u89e3\u5f00\u53d1\u4e2d\u6700\u5e38\u7528\u7684 API\uff0c\u5e76\u642d\u914d\u5b8c\u6574\u793a\u4f8b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u53bb\u91cd distinct ()\uff08\u4e2d\u95f4\u64cd\u4f5c\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>distinct()<\/code> \u4f1a\u53bb\u9664\u6d41\u4e2d\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u65b0\u6d41\uff0c\u5e38\u642d\u914d\u7ec8\u7aef\u64cd\u4f5c <code>count()<\/code>\uff08\u7edf\u8ba1\u5143\u7d20\u4e2a\u6570\uff09\u4f7f\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\nimport java.util.List;\n\npublic class StreamDemo {\n    public static void main(String&#91;] args) {\n        List&lt;Integer&gt; numList = new ArrayList&lt;&gt;();\n        numList.add(1);\n        numList.add(2);\n        numList.add(2);\n        numList.add(3);\n        numList.add(3);\n\n        \/\/ \u53bb\u91cd + \u7edf\u8ba1\u5143\u7d20\u603b\u6570\n        long count = numList.stream()\n                .distinct() \/\/ \u4e2d\u95f4\u64cd\u4f5c\uff1a\u53bb\u91cd\n                .count();    \/\/ \u7ec8\u7aef\u64cd\u4f5c\uff1a\u7edf\u8ba1\u6570\u91cf\n        System.out.println(\"\u53bb\u91cd\u540e\u5143\u7d20\u4e2a\u6570\uff1a\" + count); \/\/ \u8f93\u51fa\uff1a3\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u8fc7\u6ee4 filter ()\uff08\u4e2d\u95f4\u64cd\u4f5c\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6309\u7167\u6307\u5b9a\u6761\u4ef6\u7b5b\u9009\u5143\u7d20\uff0c\u53ea\u4fdd\u7559\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\nimport java.util.List;\n\npublic class StreamDemo {\n    public static void main(String&#91;] args) {\n        List&lt;Integer&gt; numList = List.of(1, 2, 3, 4, 5, 6);\n\n        \/\/ \u7b5b\u9009\u5927\u4e8e3\u7684\u5143\u7d20\u5e76\u904d\u5386\u8f93\u51fa\n        numList.stream()\n                .filter(n -&gt; n &gt; 3) \/\/ \u8fc7\u6ee4\u6761\u4ef6\n                .forEach(System.out::println); \/\/ \u7ec8\u7aef\u64cd\u4f5c\uff1a\u904d\u5386\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u6620\u5c04 map ()\uff08\u4e2d\u95f4\u64cd\u4f5c\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06\u6d41\u4e2d\u7684\u5143\u7d20<strong>\u8f6c\u6362\u4e3a\u65b0\u7c7b\u578b \/ \u65b0\u6570\u636e<\/strong>\uff0c\u6bd4\u5982\u7c7b\u578b\u8f6c\u6362\u3001\u5b57\u6bb5\u63d0\u53d6\u3001\u6570\u636e\u8fd0\u7b97\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.List;\n\npublic class StreamDemo {\n    public static void main(String&#91;] args) {\n        List&lt;String&gt; strList = List.of(\"1\", \"2\", \"3\", \"4\");\n\n        \/\/ \u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u6574\u6570\n        strList.stream()\n                .map(Integer::parseInt) \/\/ \u6620\u5c04\u8f6c\u6362\n                .forEach(System.out::println);\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u5143\u7d20\u5339\u914d\uff08\u7ec8\u7aef\u64cd\u4f5c\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Stream \u63d0\u4f9b\u4e09\u79cd\u5339\u914d\u65b9\u6cd5\uff0c\u8fd4\u56de\u5e03\u5c14\u503c\uff0c\u5c5e\u4e8e\u7ec8\u7aef\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>anyMatch()<\/code>\uff1a\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u6ee1\u8db3\u6761\u4ef6\uff0c\u8fd4\u56de true\uff1b<\/li>\n\n\n\n<li><code>allMatch()<\/code>\uff1a<strong>\u6240\u6709<\/strong>\u5143\u7d20\u90fd\u6ee1\u8db3\u6761\u4ef6\uff0c\u624d\u8fd4\u56de true\uff1b<\/li>\n\n\n\n<li><code>noneMatch()<\/code>\uff1a<strong>\u6240\u6709<\/strong>\u5143\u7d20\u90fd\u4e0d\u6ee1\u8db3\u6761\u4ef6\uff0c\u624d\u8fd4\u56de true\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.List;\n\npublic class StreamDemo {\n    public static void main(String&#91;] args) {\n        List&lt;Integer&gt; numList = List.of(2, 4, 6, 8);\n\n        \/\/ \u662f\u5426\u5b58\u5728\u5927\u4e8e5\u7684\u5143\u7d20\n        boolean any = numList.stream().anyMatch(n -&gt; n &gt; 5);\n        \/\/ \u662f\u5426\u5168\u90e8\u662f\u5076\u6570\n        boolean all = numList.stream().allMatch(n -&gt; n % 2 == 0);\n        \/\/ \u662f\u5426\u6ca1\u6709\u5927\u4e8e10\u7684\u5143\u7d20\n        boolean none = numList.stream().noneMatch(n -&gt; n &gt; 10);\n\n        System.out.println(any);   \/\/ true\n        System.out.println(all);   \/\/ true\n        System.out.println(none);  \/\/ true\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u5f52\u7ea6\u7ec4\u5408 reduce ()\uff08\u7ec8\u7aef\u64cd\u4f5c\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>reduce()<\/code> \u7528\u4e8e\u5c06\u6d41\u4e2d\u6240\u6709\u5143\u7d20<strong>\u8fed\u4ee3\u5408\u5e76\u3001\u8ba1\u7b97<\/strong>\uff0c\u5e38\u7528\u4e8e\u6c42\u548c\u3001\u6c42\u4e58\u79ef\u3001\u53d6\u6700\u503c\u7b49\u573a\u666f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.List;\n\npublic class StreamDemo {\n    public static void main(String&#91;] args) {\n        List&lt;Integer&gt; numList = List.of(1, 2, 3, 4, 5);\n\n        \/\/ \u7d2f\u52a0\u6c42\u548c\n        numList.stream()\n                .reduce(Integer::sum)\n                .ifPresent(sum -&gt; System.out.println(\"\u603b\u548c\uff1a\" + sum)); \/\/ \u8f93\u51fa\uff1a15\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u6d41\u8f6c\u6362\u4e3a\u6570\u7ec4 \/ \u96c6\u5408<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5904\u7406\u5b8c\u6d41\u6570\u636e\u540e\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u628a\u6d41\u8fd8\u539f\u4e3a\u6570\u7ec4\u6216\u96c6\u5408\uff0c\u4e3b\u8981\u4f7f\u7528 <code>toArray()<\/code> \u548c <code>collect()<\/code> \u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u6d41\u8f6c\u6570\u7ec4 toArray ()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.List;\n\npublic class StreamDemo {\n    public static void main(String&#91;] args) {\n        List&lt;String&gt; list = List.of(\"Java\", \"MySQL\", \"Linux\");\n        \/\/ \u6d41\u8f6c\u6570\u7ec4\n        String&#91;] array = list.stream().toArray(String&#91;]::new);\n        for (String s : array) {\n            System.out.println(s);\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u6d41\u8f6c\u96c6\u5408 collect ()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u501f\u52a9 <code>Collectors<\/code> \u5de5\u5177\u7c7b\uff0c\u53ef\u4ee5\u5c06\u6d41\u6536\u96c6\u4e3a <code>List<\/code>\u3001<code>Set<\/code> \u7b49\u4e0d\u540c\u96c6\u5408\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.List;\nimport java.util.stream.Collectors;\n\npublic class StreamDemo {\n    public static void main(String&#91;] args) {\n        List&lt;Integer&gt; numList = List.of(1, 2, 3, 4, 5);\n\n        \/\/ \u8fc7\u6ee4\u540e\u91cd\u65b0\u6536\u96c6\u4e3aList\u96c6\u5408\n        List&lt;Integer&gt; newList = numList.stream()\n                .filter(n -&gt; n % 2 == 0)\n                .collect(Collectors.toList());\n\n        System.out.println(newList); \/\/ \u8f93\u51fa &#91;2, 4]\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u603b\u7ed3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Stream API \u6781\u5927\u7b80\u5316\u4e86\u96c6\u5408\u4e0e\u6570\u7ec4\u7684\u5904\u7406\u903b\u8f91\uff0c\u7ed3\u5408 Lambda \u8868\u8fbe\u5f0f\u540e\u4ee3\u7801\u66f4\u52a0\u7cbe\u7b80\u3002\u4f7f\u7528\u65f6\u8bb0\u4f4f\u4e24\u4e2a\u5173\u952e\u70b9\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4e2d\u95f4\u64cd\u4f5c\u5ef6\u8fdf\u6267\u884c\uff0c<strong>\u5fc5\u987b\u914d\u5408\u7ec8\u7aef\u64cd\u4f5c<\/strong>\u624d\u4f1a\u89e6\u53d1\u6574\u4e2a\u6d41\u7684\u6267\u884c\uff1b<\/li>\n\n\n\n<li>\u6d41\u53ea\u80fd\u4f7f\u7528\u4e00\u6b21\uff0c\u7ec8\u7aef\u64cd\u4f5c\u6267\u884c\u5b8c\u6bd5\u540e\u6d41\u81ea\u52a8\u5173\u95ed\uff0c\u4e0d\u53ef\u91cd\u590d\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u540e\u7eed\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0 Java \u8fdb\u9636\u76f8\u5173\u5185\u5bb9\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6301\u7eed\u5173\u6ce8\uff0c\u6709\u95ee\u9898\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u4ea4\u6d41\uff5e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u7ee7\u7eed\u5206\u4eab Java \u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u672c\u7bc7\u6765\u8be6\u7ec6\u8bb2\u89e3 Stream API\u3002Stream \u662f Java <\/p>\n<div class=\"more-link\">\n\t\t\t\t <a href=\"https:\/\/yunyanglib.cn\/?p=1551\" class=\"link-btn theme-btn\"><span>Read More <\/span> <i class=\"fa fa-caret-right\"><\/i><\/a>\n\t\t\t<\/div>\n","protected":false},"author":1,"featured_media":1552,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[15],"class_list":["post-1551","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java"],"_links":{"self":[{"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/posts\/1551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1551"}],"version-history":[{"count":2,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/posts\/1551\/revisions"}],"predecessor-version":[{"id":1554,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/posts\/1551\/revisions\/1554"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/media\/1552"}],"wp:attachment":[{"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}