{"id":1097,"date":"2020-01-15T10:34:32","date_gmt":"2020-01-15T02:34:32","guid":{"rendered":"http:\/\/www.rain1024.com\/?p=1097"},"modified":"2023-08-07T20:57:24","modified_gmt":"2023-08-07T12:57:24","slug":"java-8-%e4%b8%ad%e7%9a%84-streams-api-%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2020\/01\/15\/java-8-%e4%b8%ad%e7%9a%84-streams-api-%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Java 8 \u4e2d\u7684 Streams API \u8be6\u89e3"},"content":{"rendered":"<h1>Java 8 \u4e2d\u7684 Streams API \u8be6\u89e3<\/h1>\n<p>Streams \u7684\u80cc\u666f\uff0c\u4ee5\u53ca Java 8 \u4e2d\u7684\u4f7f\u7528\u8be6\u89e3<\/p>\n<h2>\u4e3a\u4ec0\u4e48\u9700\u8981 Stream<\/h2>\n<p>Stream \u4f5c\u4e3a Java 8 \u7684\u4e00\u5927\u4eae\u70b9\uff0c\u5b83\u4e0e java.io \u5305\u91cc\u7684 InputStream \u548c OutputStream \u662f\u5b8c\u5168\u4e0d\u540c\u7684\u6982\u5ff5\u3002\u5b83\u4e5f\u4e0d\u540c\u4e8e StAX \u5bf9 XML \u89e3\u6790\u7684 Stream\uff0c\u4e5f\u4e0d\u662f Amazon Kinesis \u5bf9\u5927\u6570\u636e\u5b9e\u65f6\u5904\u7406\u7684 Stream\u3002Java 8 \u4e2d\u7684 Stream \u662f\u5bf9\u96c6\u5408\uff08Collection\uff09\u5bf9\u8c61\u529f\u80fd\u7684\u589e\u5f3a\uff0c\u5b83\u4e13\u6ce8\u4e8e\u5bf9\u96c6\u5408\u5bf9\u8c61\u8fdb\u884c\u5404\u79cd\u975e\u5e38\u4fbf\u5229\u3001\u9ad8\u6548\u7684\u805a\u5408\u64cd\u4f5c\uff08aggregate operation\uff09\uff0c\u6216\u8005\u5927\u6279\u91cf\u6570\u636e\u64cd\u4f5c (bulk data operation)\u3002Stream API \u501f\u52a9\u4e8e\u540c\u6837\u65b0\u51fa\u73b0\u7684 Lambda \u8868\u8fbe\u5f0f\uff0c\u6781\u5927\u7684\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u548c\u7a0b\u5e8f\u53ef\u8bfb\u6027\u3002\u540c\u65f6\u5b83\u63d0\u4f9b\u4e32\u884c\u548c\u5e76\u884c\u4e24\u79cd\u6a21\u5f0f\u8fdb\u884c\u6c47\u805a\u64cd\u4f5c\uff0c\u5e76\u53d1\u6a21\u5f0f\u80fd\u591f\u5145\u5206\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u7684\u4f18\u52bf\uff0c\u4f7f\u7528 fork\/join \u5e76\u884c\u65b9\u5f0f\u6765\u62c6\u5206\u4efb\u52a1\u548c\u52a0\u901f\u5904\u7406\u8fc7\u7a0b\u3002\u901a\u5e38\u7f16\u5199\u5e76\u884c\u4ee3\u7801\u5f88\u96be\u800c\u4e14\u5bb9\u6613\u51fa\u9519, \u4f46\u4f7f\u7528 Stream API \u65e0\u9700\u7f16\u5199\u4e00\u884c\u591a\u7ebf\u7a0b\u7684\u4ee3\u7801\uff0c\u5c31\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5199\u51fa\u9ad8\u6027\u80fd\u7684\u5e76\u53d1\u7a0b\u5e8f\u3002\u6240\u4ee5\u8bf4\uff0cJava 8 \u4e2d\u9996\u6b21\u51fa\u73b0\u7684 java.util.stream \u662f\u4e00\u4e2a\u51fd\u6570\u5f0f\u8bed\u8a00+\u591a\u6838\u65f6\u4ee3\u7efc\u5408\u5f71\u54cd\u7684\u4ea7\u7269\u3002<\/p>\n<h3>\u4ec0\u4e48\u662f\u805a\u5408\u64cd\u4f5c<\/h3>\n<p>\u5728\u4f20\u7edf\u7684 J2EE \u5e94\u7528\u4e2d\uff0cJava \u4ee3\u7801\u7ecf\u5e38\u4e0d\u5f97\u4e0d\u4f9d\u8d56\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7684\u805a\u5408\u64cd\u4f5c\u6765\u5b8c\u6210\u8bf8\u5982\uff1a<\/p>\n<ul>\n<li>\u5ba2\u6237\u6bcf\u6708\u5e73\u5747\u6d88\u8d39\u91d1\u989d<\/li>\n<li>\u6700\u6602\u8d35\u7684\u5728\u552e\u5546\u54c1<\/li>\n<li>\u672c\u5468\u5b8c\u6210\u7684\u6709\u6548\u8ba2\u5355\uff08\u6392\u9664\u4e86\u65e0\u6548\u7684\uff09<\/li>\n<li>\u53d6\u5341\u4e2a\u6570\u636e\u6837\u672c\u4f5c\u4e3a\u9996\u9875\u63a8\u8350<\/li>\n<\/ul>\n<p>\u8fd9\u7c7b\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u4f46\u5728\u5f53\u4eca\u8fd9\u4e2a\u6570\u636e\u5927\u7206\u70b8\u7684\u65f6\u4ee3\uff0c\u5728\u6570\u636e\u6765\u6e90\u591a\u6837\u5316\u3001\u6570\u636e\u6d77\u91cf\u5316\u7684\u4eca\u5929\uff0c\u5f88\u591a\u65f6\u5019\u4e0d\u5f97\u4e0d\u8131\u79bb RDBMS\uff0c\u6216\u8005\u4ee5\u5e95\u5c42\u8fd4\u56de\u7684\u6570\u636e\u4e3a\u57fa\u7840\u8fdb\u884c\u66f4\u4e0a\u5c42\u7684\u6570\u636e\u7edf\u8ba1\u3002\u800c Java \u7684\u96c6\u5408 API \u4e2d\uff0c\u4ec5\u4ec5\u6709\u6781\u5c11\u91cf\u7684\u8f85\u52a9\u578b\u65b9\u6cd5\uff0c\u66f4\u591a\u7684\u65f6\u5019\u662f\u7a0b\u5e8f\u5458\u9700\u8981\u7528 Iterator \u6765\u904d\u5386\u96c6\u5408\uff0c\u5b8c\u6210\u76f8\u5173\u7684\u805a\u5408\u5e94\u7528\u903b\u8f91\u3002\u8fd9\u662f\u4e00\u79cd\u8fdc\u4e0d\u591f\u9ad8\u6548\u3001\u7b28\u62d9\u7684\u65b9\u6cd5\u3002\u5728 Java 7 \u4e2d\uff0c\u5982\u679c\u8981\u53d1\u73b0 type \u4e3a grocery \u7684\u6240\u6709\u4ea4\u6613\uff0c\u7136\u540e\u8fd4\u56de\u4ee5\u4ea4\u6613\u503c\u964d\u5e8f\u6392\u5e8f\u597d\u7684\u4ea4\u6613 ID \u96c6\u5408\uff0c\u6211\u4eec\u9700\u8981\u8fd9\u6837\u5199\uff1a<\/p>\n<h5>\u6e05\u5355 1. Java 7 \u7684\u6392\u5e8f\u3001\u53d6\u503c\u5b9e\u73b0<\/h5>\n<pre><code>List&lt;Transaction&gt; groceryTransactions = new Arraylist&lt;&gt;();\nfor(Transaction t: transactions){\n if(t.getType() == Transaction.GROCERY){\n groceryTransactions.add(t);\n }\n}\nCollections.sort(groceryTransactions, new Comparator(){\n public int compare(Transaction t1, Transaction t2){\n return t2.getValue().compareTo(t1.getValue());\n }\n});\nList&lt;Integer&gt; transactionIds = new ArrayList&lt;&gt;();\nfor(Transaction t: groceryTransactions){\n transactionsIds.add(t.getId());\n}\n<\/code><\/pre>\n<p>\u800c\u5728 Java 8 \u4f7f\u7528 Stream\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\uff1b\u800c\u4e14\u4f7f\u7528\u5e76\u53d1\u6a21\u5f0f\uff0c\u7a0b\u5e8f\u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002<\/p>\n<h5>\u6e05\u5355 2. Java 8 \u7684\u6392\u5e8f\u3001\u53d6\u503c\u5b9e\u73b0<\/h5>\n<pre><code>List&lt;Integer&gt; transactionsIds = transactions.parallelStream().\n filter(t -&gt; t.getType() == Transaction.GROCERY).\n sorted(comparing(Transaction::getValue).reversed()).\n map(Transaction::getId).\n collect(toList());\n<\/code><\/pre>\n<p>Stream \u4e0d\u662f\u96c6\u5408\u5143\u7d20\uff0c\u5b83\u4e0d\u662f\u6570\u636e\u7ed3\u6784\u5e76\u4e0d\u4fdd\u5b58\u6570\u636e\uff0c\u5b83\u662f\u6709\u5173\u7b97\u6cd5\u548c\u8ba1\u7b97\u7684\uff0c\u5b83\u66f4\u50cf\u4e00\u4e2a\u9ad8\u7ea7\u7248\u672c\u7684 Iterator\u3002\u539f\u59cb\u7248\u672c\u7684 Iterator\uff0c\u7528\u6237\u53ea\u80fd\u663e\u5f0f\u5730\u4e00\u4e2a\u4e00\u4e2a\u904d\u5386\u5143\u7d20\u5e76\u5bf9\u5176\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\uff1b\u9ad8\u7ea7\u7248\u672c\u7684 Stream\uff0c\u7528\u6237\u53ea\u8981\u7ed9\u51fa\u9700\u8981\u5bf9\u5176\u5305\u542b\u7684\u5143\u7d20\u6267\u884c\u4ec0\u4e48\u64cd\u4f5c\uff0c\u6bd4\u5982 \u201c\u8fc7\u6ee4\u6389\u957f\u5ea6\u5927\u4e8e 10 \u7684\u5b57\u7b26\u4e32\u201d\u3001\u201c\u83b7\u53d6\u6bcf\u4e2a\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u201d\u7b49\uff0cStream \u4f1a\u9690\u5f0f\u5730\u5728\u5185\u90e8\u8fdb\u884c\u904d\u5386\uff0c\u505a\u51fa\u76f8\u5e94\u7684\u6570\u636e\u8f6c\u6362\u3002<\/p>\n<p>Stream \u5c31\u5982\u540c\u4e00\u4e2a\u8fed\u4ee3\u5668\uff08Iterator\uff09\uff0c\u5355\u5411\uff0c\u4e0d\u53ef\u5f80\u590d\uff0c\u6570\u636e\u53ea\u80fd\u904d\u5386\u4e00\u6b21\uff0c\u904d\u5386\u8fc7\u4e00\u6b21\u540e\u5373\u7528\u5c3d\u4e86\uff0c\u5c31\u597d\u6bd4\u6d41\u6c34\u4ece\u9762\u524d\u6d41\u8fc7\uff0c\u4e00\u53bb\u4e0d\u590d\u8fd4\u3002<\/p>\n<p>\u800c\u548c\u8fed\u4ee3\u5668\u53c8\u4e0d\u540c\u7684\u662f\uff0cStream \u53ef\u4ee5\u5e76\u884c\u5316\u64cd\u4f5c\uff0c\u8fed\u4ee3\u5668\u53ea\u80fd\u547d\u4ee4\u5f0f\u5730\u3001\u4e32\u884c\u5316\u64cd\u4f5c\u3002\u987e\u540d\u601d\u4e49\uff0c\u5f53\u4f7f\u7528\u4e32\u884c\u65b9\u5f0f\u53bb\u904d\u5386\u65f6\uff0c\u6bcf\u4e2a item \u8bfb\u5b8c\u540e\u518d\u8bfb\u4e0b\u4e00\u4e2a item\u3002\u800c\u4f7f\u7528\u5e76\u884c\u53bb\u904d\u5386\u65f6\uff0c\u6570\u636e\u4f1a\u88ab\u5206\u6210\u591a\u4e2a\u6bb5\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u90fd\u5728\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e2d\u5904\u7406\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u4e00\u8d77\u8f93\u51fa\u3002Stream \u7684\u5e76\u884c\u64cd\u4f5c\u4f9d\u8d56\u4e8e Java7 \u4e2d\u5f15\u5165\u7684 Fork\/Join \u6846\u67b6\uff08JSR166y\uff09\u6765\u62c6\u5206\u4efb\u52a1\u548c\u52a0\u901f\u5904\u7406\u8fc7\u7a0b\u3002Java \u7684\u5e76\u884c API \u6f14\u53d8\u5386\u7a0b\u57fa\u672c\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>1.0-1.4 \u4e2d\u7684 java.lang.Thread<\/li>\n<li>5.0 \u4e2d\u7684 java.util.concurrent<\/li>\n<li>6.0 \u4e2d\u7684 Phasers \u7b49<\/li>\n<li>7.0 \u4e2d\u7684 Fork\/Join \u6846\u67b6<\/li>\n<li>8.0 \u4e2d\u7684 Lambda<\/li>\n<\/ol>\n<p>Stream \u7684\u53e6\u5916\u4e00\u5927\u7279\u70b9\u662f\uff0c\u6570\u636e\u6e90\u672c\u8eab\u53ef\u4ee5\u662f\u65e0\u9650\u7684\u3002<\/p>\n<h3>\u6d41\u7684\u6784\u6210<\/h3>\n<p>\u5f53\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u6d41\u7684\u65f6\u5019\uff0c\u901a\u5e38\u5305\u62ec\u4e09\u4e2a\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<p>\u83b7\u53d6\u4e00\u4e2a\u6570\u636e\u6e90\uff08source\uff09\u2192 \u6570\u636e\u8f6c\u6362\u2192\u6267\u884c\u64cd\u4f5c\u83b7\u53d6\u60f3\u8981\u7684\u7ed3\u679c\uff0c\u6bcf\u6b21\u8f6c\u6362\u539f\u6709 Stream \u5bf9\u8c61\u4e0d\u6539\u53d8\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 Stream \u5bf9\u8c61\uff08\u53ef\u4ee5\u6709\u591a\u6b21\u8f6c\u6362\uff09\uff0c\u8fd9\u5c31\u5141\u8bb8\u5bf9\u5176\u64cd\u4f5c\u53ef\u4ee5\u50cf\u94fe\u6761\u4e00\u6837\u6392\u5217\uff0c\u53d8\u6210\u4e00\u4e2a\u7ba1\u9053\uff0c<\/p>\n<p>\u6709\u591a\u79cd\u65b9\u5f0f\u751f\u6210 Stream Source\uff1a<\/p>\n<pre><code>* Collection.stream()\n* Collection.parallelStream()\n* Arrays.stream(T array) or Stream.of()\n<\/code><\/pre>\n<p>\u4ece BufferedReader<\/p>\n<pre><code>* java.io.BufferedReader.lines()\n\n* java.util.stream.IntStream.range()\n* java.nio.file.Files.walk()\n<\/code><\/pre>\n<ul>\n<li>\u5176\u5b83\n<ul>\n<li>Random.ints()<\/li>\n<li>BitSet.stream()<\/li>\n<li>Pattern.splitAsStream(java.lang.CharSequence)<\/li>\n<li>JarFile.stream()<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u6d41\u7684\u64cd\u4f5c\u7c7b\u578b\u5206\u4e3a\u4e24\u79cd\uff1a<\/p>\n<ul>\n<li><strong>Intermediate<\/strong> \uff1a\u4e00\u4e2a\u6d41\u53ef\u4ee5\u540e\u9762\u8ddf\u968f\u96f6\u4e2a\u6216\u591a\u4e2a intermediate \u64cd\u4f5c\u3002\u5176\u76ee\u7684\u4e3b\u8981\u662f\u6253\u5f00\u6d41\uff0c\u505a\u51fa\u67d0\u79cd\u7a0b\u5ea6\u7684\u6570\u636e\u6620\u5c04\/\u8fc7\u6ee4\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6d41\uff0c\u4ea4\u7ed9\u4e0b\u4e00\u4e2a\u64cd\u4f5c\u4f7f\u7528\u3002\u8fd9\u7c7b\u64cd\u4f5c\u90fd\u662f\u60f0\u6027\u5316\u7684\uff08lazy\uff09\uff0c\u5c31\u662f\u8bf4\uff0c\u4ec5\u4ec5\u8c03\u7528\u5230\u8fd9\u7c7b\u65b9\u6cd5\uff0c\u5e76\u6ca1\u6709\u771f\u6b63\u5f00\u59cb\u6d41\u7684\u904d\u5386\u3002<\/li>\n<li><strong>Terminal<\/strong> \uff1a\u4e00\u4e2a\u6d41\u53ea\u80fd\u6709\u4e00\u4e2a terminal \u64cd\u4f5c\uff0c\u5f53\u8fd9\u4e2a\u64cd\u4f5c\u6267\u884c\u540e\uff0c\u6d41\u5c31\u88ab\u4f7f\u7528\u201c\u5149\u201d\u4e86\uff0c\u65e0\u6cd5\u518d\u88ab\u64cd\u4f5c\u3002\u6240\u4ee5\u8fd9\u5fc5\u5b9a\u662f\u6d41\u7684\u6700\u540e\u4e00\u4e2a\u64cd\u4f5c\u3002Terminal \u64cd\u4f5c\u7684\u6267\u884c\uff0c\u624d\u4f1a\u771f\u6b63\u5f00\u59cb\u6d41\u7684\u904d\u5386\uff0c\u5e76\u4e14\u4f1a\u751f\u6210\u4e00\u4e2a\u7ed3\u679c\uff0c\u6216\u8005\u4e00\u4e2a side effect\u3002<\/li>\n<\/ul>\n<p>\u5728\u5bf9\u4e8e\u4e00\u4e2a Stream \u8fdb\u884c\u591a\u6b21\u8f6c\u6362\u64cd\u4f5c (Intermediate \u64cd\u4f5c)\uff0c\u6bcf\u6b21\u90fd\u5bf9 Stream \u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u8f6c\u6362\uff0c\u800c\u4e14\u662f\u6267\u884c\u591a\u6b21\uff0c\u8fd9\u6837\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u662f N\uff08\u8f6c\u6362\u6b21\u6570\uff09\u4e2a for \u5faa\u73af\u91cc\u628a\u6240\u6709\u64cd\u4f5c\u90fd\u505a\u6389\u7684\u603b\u548c\u5417\uff1f\u5176\u5b9e\u4e0d\u662f\u8fd9\u6837\u7684\uff0c\u8f6c\u6362\u64cd\u4f5c\u90fd\u662f lazy \u7684\uff0c\u591a\u4e2a\u8f6c\u6362\u64cd\u4f5c\u53ea\u4f1a\u5728 Terminal \u64cd\u4f5c\u7684\u65f6\u5019\u878d\u5408\u8d77\u6765\uff0c\u4e00\u6b21\u5faa\u73af\u5b8c\u6210\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u7b80\u5355\u7684\u7406\u89e3\uff0cStream \u91cc\u6709\u4e2a\u64cd\u4f5c\u51fd\u6570\u7684\u96c6\u5408\uff0c\u6bcf\u6b21\u8f6c\u6362\u64cd\u4f5c\u5c31\u662f\u628a\u8f6c\u6362\u51fd\u6570\u653e\u5165\u8fd9\u4e2a\u96c6\u5408\u4e2d\uff0c\u5728 Terminal \u64cd\u4f5c\u7684\u65f6\u5019\u5faa\u73af Stream \u5bf9\u5e94\u7684\u96c6\u5408\uff0c\u7136\u540e\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u6240\u6709\u7684\u51fd\u6570\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u79cd\u64cd\u4f5c\u88ab\u79f0\u4e3a <strong>short-circuiting<\/strong> \u3002\u7528\u4ee5\u6307\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u4e00\u4e2a intermediate \u64cd\u4f5c\uff0c\u5982\u679c\u5b83\u63a5\u53d7\u7684\u662f\u4e00\u4e2a\u65e0\u9650\u5927\uff08infinite\/unbounded\uff09\u7684 Stream\uff0c\u4f46\u8fd4\u56de\u4e00\u4e2a\u6709\u9650\u7684\u65b0 Stream\u3002<\/li>\n<li>\u5bf9\u4e8e\u4e00\u4e2a terminal \u64cd\u4f5c\uff0c\u5982\u679c\u5b83\u63a5\u53d7\u7684\u662f\u4e00\u4e2a\u65e0\u9650\u5927\u7684 Stream\uff0c\u4f46\u80fd\u5728\u6709\u9650\u7684\u65f6\u95f4\u8ba1\u7b97\u51fa\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>\u5f53\u64cd\u4f5c\u4e00\u4e2a\u65e0\u9650\u5927\u7684 Stream\uff0c\u800c\u53c8\u5e0c\u671b\u5728\u6709\u9650\u65f6\u95f4\u5185\u5b8c\u6210\u64cd\u4f5c\uff0c\u5219\u5728\u7ba1\u9053\u5185\u62e5\u6709\u4e00\u4e2a short-circuiting \u64cd\u4f5c\u662f\u5fc5\u8981\u975e\u5145\u5206\u6761\u4ef6\u3002<\/p>\n<h5>\u6e05\u5355 3. \u4e00\u4e2a\u6d41\u64cd\u4f5c\u7684\u793a\u4f8b<\/h5>\n<pre><code>int sum = widgets.stream()\n.filter(w -&gt; w.getColor() == RED)\n .mapToInt(w -&gt; w.getWeight())\n .sum();\n<\/code><\/pre>\n<p>stream() \u83b7\u53d6\u5f53\u524d\u5c0f\u7269\u4ef6\u7684 source\uff0cfilter \u548c mapToInt \u4e3a intermediate \u64cd\u4f5c\uff0c\u8fdb\u884c\u6570\u636e\u7b5b\u9009\u548c\u8f6c\u6362\uff0c\u6700\u540e\u4e00\u4e2a sum() \u4e3a terminal \u64cd\u4f5c\uff0c\u5bf9\u7b26\u5408\u6761\u4ef6\u7684\u5168\u90e8\u5c0f\u7269\u4ef6\u4f5c\u91cd\u91cf\u6c42\u548c\u3002<\/p>\n<h2>\u6d41\u7684\u4f7f\u7528\u8be6\u89e3<\/h2>\n<p>\u7b80\u5355\u8bf4\uff0c\u5bf9 Stream \u7684\u4f7f\u7528\u5c31\u662f\u5b9e\u73b0\u4e00\u4e2a filter-map-reduce \u8fc7\u7a0b\uff0c\u4ea7\u751f\u4e00\u4e2a\u6700\u7ec8\u7ed3\u679c\uff0c\u6216\u8005\u5bfc\u81f4\u4e00\u4e2a\u526f\u4f5c\u7528\uff08side effect\uff09\u3002<\/p>\n<h3>\u6d41\u7684\u6784\u9020\u4e0e\u8f6c\u6362<\/h3>\n<p>\u4e0b\u9762\u63d0\u4f9b\u6700\u5e38\u89c1\u7684\u51e0\u79cd\u6784\u9020 Stream \u7684\u6837\u4f8b\u3002<\/p>\n<h5>\u6e05\u5355 4. \u6784\u9020\u6d41\u7684\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5<\/h5>\n<pre><code>\/\/ 1. Individual values\nStream stream = Stream.of(\"a\", \"b\", \"c\");\n\/\/ 2. Arrays\nString [] strArray = new String[] {\"a\", \"b\", \"c\"};\nstream = Stream.of(strArray);\nstream = Arrays.stream(strArray);\n\/\/ 3. Collections\nList&lt;String&gt; list = Arrays.asList(strArray);\nstream = list.stream();\n<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u4e8e\u57fa\u672c\u6570\u503c\u578b\uff0c\u76ee\u524d\u6709\u4e09\u79cd\u5bf9\u5e94\u7684\u5305\u88c5\u7c7b\u578b Stream\uff1a<\/p>\n<p>IntStream\u3001LongStream\u3001DoubleStream\u3002\u5f53\u7136\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528 Stream<Integer>\u3001Stream<Long> >\u3001Stream<Double>\uff0c\u4f46\u662f boxing \u548c unboxing \u4f1a\u5f88\u8017\u65f6\uff0c\u6240\u4ee5\u7279\u522b\u4e3a\u8fd9\u4e09\u79cd\u57fa\u672c\u6570\u503c\u578b\u63d0\u4f9b\u4e86\u5bf9\u5e94\u7684 Stream\u3002<\/p>\n<p>Java 8 \u4e2d\u8fd8\u6ca1\u6709\u63d0\u4f9b\u5176\u5b83\u6570\u503c\u578b Stream\uff0c\u56e0\u4e3a\u8fd9\u5c06\u5bfc\u81f4\u6269\u589e\u7684\u5185\u5bb9\u8f83\u591a\u3002\u800c\u5e38\u89c4\u7684\u6570\u503c\u578b\u805a\u5408\u8fd0\u7b97\u53ef\u4ee5\u901a\u8fc7\u4e0a\u9762\u4e09\u79cd Stream \u8fdb\u884c\u3002<\/p>\n<h5>\u6e05\u5355 5. \u6570\u503c\u6d41\u7684\u6784\u9020<\/h5>\n<pre><code>IntStream.of(new int[]{1, 2, 3}).forEach(System.out::println);\nIntStream.range(1, 3).forEach(System.out::println);\nIntStream.rangeClosed(1, 3).forEach(System.out::println);\n<\/code><\/pre>\n<pre><code>\/\/ 1. Array\nString[] strArray1 = stream.toArray(String[]::new);\n\/\/ 2. Collection\nList&lt;String&gt; list1 = stream.collect(Collectors.toList());\nList&lt;String&gt; list2 = stream.collect(Collectors.toCollection(ArrayList::new));\nSet set1 = stream.collect(Collectors.toSet());\nStack stack1 = stream.collect(Collectors.toCollection(Stack::new));\n\/\/ 3. String\nString str = stream.collect(Collectors.joining()).toString();\n<\/code><\/pre>\n<p>\u4e00\u4e2a Stream \u53ea\u53ef\u4ee5\u4f7f\u7528\u4e00\u6b21\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u4e3a\u4e86\u7b80\u6d01\u800c\u91cd\u590d\u4f7f\u7528\u4e86\u6570\u6b21\u3002<\/p>\n<h3>\u6d41\u7684\u64cd\u4f5c<\/h3>\n<p>\u63a5\u4e0b\u6765\uff0c\u5f53\u628a\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u5305\u88c5\u6210 Stream \u540e\uff0c\u5c31\u8981\u5f00\u59cb\u5bf9\u91cc\u9762\u7684\u5143\u7d20\u8fdb\u884c\u5404\u7c7b\u64cd\u4f5c\u4e86\u3002\u5e38\u89c1\u7684\u64cd\u4f5c\u53ef\u4ee5\u5f52\u7c7b\u5982\u4e0b\u3002<\/p>\n<p>map (mapToInt, flatMap \u7b49)\u3001 filter\u3001 distinct\u3001 sorted\u3001 peek\u3001 limit\u3001 skip\u3001 parallel\u3001 sequential\u3001 unordered<\/p>\n<p>forEach\u3001 forEachOrdered\u3001 toArray\u3001 reduce\u3001 collect\u3001 min\u3001 max\u3001 count\u3001 anyMatch\u3001 allMatch\u3001 noneMatch\u3001 findFirst\u3001 findAny\u3001 iterator<\/p>\n<p>anyMatch\u3001 allMatch\u3001 noneMatch\u3001 findFirst\u3001 findAny\u3001 limit<\/p>\n<p>\u6211\u4eec\u4e0b\u9762\u770b\u4e00\u4e0b Stream \u7684\u6bd4\u8f83\u5178\u578b\u7528\u6cd5\u3002<\/p>\n<p><strong>map\/flatMap<\/strong><\/p>\n<p>\u6211\u4eec\u5148\u6765\u770b map\u3002\u5982\u679c\u4f60\u719f\u6089 scala \u8fd9\u7c7b\u51fd\u6570\u5f0f\u8bed\u8a00\uff0c\u5bf9\u8fd9\u4e2a\u65b9\u6cd5\u5e94\u8be5\u5f88\u4e86\u89e3\uff0c\u5b83\u7684\u4f5c\u7528\u5c31\u662f\u628a input Stream \u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u6620\u5c04\u6210 output Stream \u7684\u53e6\u5916\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<h5>\u6e05\u5355 7. \u8f6c\u6362\u5927\u5199<\/h5>\n<pre><code>List&lt;String&gt; output = wordList.stream().\nmap(String::toUpperCase).\ncollect(Collectors.toList());\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u628a\u6240\u6709\u7684\u5355\u8bcd\u8f6c\u6362\u4e3a\u5927\u5199\u3002<\/p>\n<h5>\u6e05\u5355 8. \u5e73\u65b9\u6570<\/h5>\n<pre><code>List&lt;Integer&gt; nums = Arrays.asList(1, 2, 3, 4);\nList&lt;Integer&gt; squareNums = nums.stream().\nmap(n -&gt; n * n).\ncollect(Collectors.toList());\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u751f\u6210\u4e00\u4e2a\u6574\u6570 list \u7684\u5e73\u65b9\u6570 {1, 4, 9, 16}\u3002<\/p>\n<p>\u4ece\u4e0a\u9762\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0cmap \u751f\u6210\u7684\u662f\u4e2a 1:1 \u6620\u5c04\uff0c\u6bcf\u4e2a\u8f93\u5165\u5143\u7d20\uff0c\u90fd\u6309\u7167\u89c4\u5219\u8f6c\u6362\u6210\u4e3a\u53e6\u5916\u4e00\u4e2a\u5143\u7d20\u3002\u8fd8\u6709\u4e00\u4e9b\u573a\u666f\uff0c\u662f\u4e00\u5bf9\u591a\u6620\u5c04\u5173\u7cfb\u7684\uff0c\u8fd9\u65f6\u9700\u8981 flatMap\u3002<\/p>\n<h5>\u6e05\u5355 9. \u4e00\u5bf9\u591a<\/h5>\n<pre><code>Stream&lt;List&lt;Integer&gt;&gt; inputStream = Stream.of(\n Arrays.asList(1),\n Arrays.asList(2, 3),\n Arrays.asList(4, 5, 6)\n );\nStream&lt;Integer&gt; outputStream = inputStream.\nflatMap((childList) -&gt; childList.stream());\n<\/code><\/pre>\n<p>flatMap \u628a input Stream \u4e2d\u7684\u5c42\u7ea7\u7ed3\u6784\u6241\u5e73\u5316\uff0c\u5c31\u662f\u5c06\u6700\u5e95\u5c42\u5143\u7d20\u62bd\u51fa\u6765\u653e\u5230\u4e00\u8d77\uff0c\u6700\u7ec8 output \u7684\u65b0 Stream \u91cc\u9762\u5df2\u7ecf\u6ca1\u6709 List \u4e86\uff0c\u90fd\u662f\u76f4\u63a5\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>filter<\/strong><\/p>\n<p>filter \u5bf9\u539f\u59cb Stream \u8fdb\u884c\u67d0\u9879\u6d4b\u8bd5\uff0c\u901a\u8fc7\u6d4b\u8bd5\u7684\u5143\u7d20\u88ab\u7559\u4e0b\u6765\u751f\u6210\u4e00\u4e2a\u65b0 Stream\u3002<\/p>\n<h5>\u6e05\u5355 10. \u7559\u4e0b\u5076\u6570<\/h5>\n<pre><code>Integer[] sixNums = {1, 2, 3, 4, 5, 6};\nInteger[] evens =\nStream.of(sixNums).filter(n -&gt; n%2 == 0).toArray(Integer[]::new);\n<\/code><\/pre>\n<p>\u7ecf\u8fc7\u6761\u4ef6\u201c\u88ab 2 \u6574\u9664\u201d\u7684 filter\uff0c\u5269\u4e0b\u7684\u6570\u5b57\u4e3a {2, 4, 6}\u3002<\/p>\n<h5>\u6e05\u5355 11. \u628a\u5355\u8bcd\u6311\u51fa\u6765<\/h5>\n<pre><code>List&lt;String&gt; output = reader.lines().\n flatMap(line -&gt; Stream.of(line.split(REGEXP))).\n filter(word -&gt; word.length() &gt; 0).\n collect(Collectors.toList());\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u9996\u5148\u628a\u6bcf\u884c\u7684\u5355\u8bcd\u7528 flatMap \u6574\u7406\u5230\u65b0\u7684 Stream\uff0c\u7136\u540e\u4fdd\u7559\u957f\u5ea6\u4e0d\u4e3a 0 \u7684\uff0c\u5c31\u662f\u6574\u7bc7\u6587\u7ae0\u4e2d\u7684\u5168\u90e8\u5355\u8bcd\u4e86\u3002<\/p>\n<p><strong>forEach<\/strong><\/p>\n<p>forEach \u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a Lambda \u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u5728 Stream \u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u4e0a\u6267\u884c\u8be5\u8868\u8fbe\u5f0f\u3002<\/p>\n<h5>\u6e05\u5355 12. \u6253\u5370\u59d3\u540d\uff08forEach \u548c pre-java8 \u7684\u5bf9\u6bd4\uff09<\/h5>\n<pre><code>\/\/ Java 8\nroster.stream()\n .filter(p -&gt; p.getGender() == Person.Sex.MALE)\n .forEach(p -&gt; System.out.println(p.getName()));\n\/\/ Pre-Java 8\nfor (Person p : roster) {\n if (p.getGender() == Person.Sex.MALE) {\n System.out.println(p.getName());\n }\n}\n<\/code><\/pre>\n<p>\u5bf9\u4e00\u4e2a\u4eba\u5458\u96c6\u5408\u904d\u5386\uff0c\u627e\u51fa\u7537\u6027\u5e76\u6253\u5370\u59d3\u540d\u3002\u53ef\u4ee5\u770b\u51fa\u6765\uff0cforEach \u662f\u4e3a Lambda \u800c\u8bbe\u8ba1\u7684\uff0c\u4fdd\u6301\u4e86\u6700\u7d27\u51d1\u7684\u98ce\u683c\u3002\u800c\u4e14 Lambda \u8868\u8fbe\u5f0f\u672c\u8eab\u662f\u53ef\u4ee5\u91cd\u7528\u7684\uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u5f53\u9700\u8981\u4e3a\u591a\u6838\u7cfb\u7edf\u4f18\u5316\u65f6\uff0c\u53ef\u4ee5 parallelStream().forEach()\uff0c\u53ea\u662f\u6b64\u65f6\u539f\u6709\u5143\u7d20\u7684\u6b21\u5e8f\u6ca1\u6cd5\u4fdd\u8bc1\uff0c\u5e76\u884c\u7684\u60c5\u51b5\u4e0b\u5c06\u6539\u53d8\u4e32\u884c\u65f6\u64cd\u4f5c\u7684\u884c\u4e3a\uff0c\u6b64\u65f6 forEach \u672c\u8eab\u7684\u5b9e\u73b0\u4e0d\u9700\u8981\u8c03\u6574\uff0c\u800c Java8 \u4ee5\u524d\u7684 for \u5faa\u73af code \u53ef\u80fd\u9700\u8981\u52a0\u5165\u989d\u5916\u7684\u591a\u7ebf\u7a0b\u903b\u8f91\u3002<\/p>\n<p>\u4f46\u4e00\u822c\u8ba4\u4e3a\uff0cforEach \u548c\u5e38\u89c4 for \u5faa\u73af\u7684\u5dee\u5f02\u4e0d\u6d89\u53ca\u5230\u6027\u80fd\uff0c\u5b83\u4eec\u4ec5\u4ec5\u662f\u51fd\u6570\u5f0f\u98ce\u683c\u4e0e\u4f20\u7edf Java \u98ce\u683c\u7684\u5dee\u522b\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u70b9\u9700\u8981\u6ce8\u610f\uff0cforEach \u662f terminal \u64cd\u4f5c\uff0c\u56e0\u6b64\u5b83\u6267\u884c\u540e\uff0cStream \u7684\u5143\u7d20\u5c31\u88ab\u201c\u6d88\u8d39\u201d\u6389\u4e86\uff0c\u4f60\u65e0\u6cd5\u5bf9\u4e00\u4e2a Stream \u8fdb\u884c\u4e24\u6b21 terminal \u8fd0\u7b97\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u9519\u8bef\u7684\uff1a<\/p>\n<pre><code>stream.forEach(element -&gt; doOneThing(element));\n     stream.forEach(element -&gt; doAnotherThing(element));\n<\/code><\/pre>\n<p>\u76f8\u53cd\uff0c\u5177\u6709\u76f8\u4f3c\u529f\u80fd\u7684 intermediate \u64cd\u4f5c peek \u53ef\u4ee5\u8fbe\u5230\u4e0a\u8ff0\u76ee\u7684\u3002\u5982\u4e0b\u662f\u51fa\u73b0\u5728\u8be5 api javadoc \u4e0a\u7684\u4e00\u4e2a\u793a\u4f8b\u3002<\/p>\n<h5>\u6e05\u5355 13. peek \u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u64cd\u4f5c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 Stream<\/h5>\n<pre><code>Stream.of(\"one\", \"two\", \"three\", \"four\")\n .filter(e -&gt; e.length() &gt; 3)\n .peek(e -&gt; System.out.println(\"Filtered value: \" + e))\n .map(String::toUpperCase)\n .peek(e -&gt; System.out.println(\"Mapped value: \" + e))\n .collect(Collectors.toList());\n<\/code><\/pre>\n<p>forEach \u4e0d\u80fd\u4fee\u6539\u81ea\u5df1\u5305\u542b\u7684\u672c\u5730\u53d8\u91cf\u503c\uff0c\u4e5f\u4e0d\u80fd\u7528 break\/return \u4e4b\u7c7b\u7684\u5173\u952e\u5b57\u63d0\u524d\u7ed3\u675f\u5faa\u73af\u3002<\/p>\n<p><strong>findFirst<\/strong><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a termimal \u517c short-circuiting \u64cd\u4f5c\uff0c\u5b83\u603b\u662f\u8fd4\u56de Stream \u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u6216\u8005\u7a7a\u3002<\/p>\n<p>\u8fd9\u91cc\u6bd4\u8f83\u91cd\u70b9\u7684\u662f\u5b83\u7684\u8fd4\u56de\u503c\u7c7b\u578b\uff1aOptional\u3002\u8fd9\u4e5f\u662f\u4e00\u4e2a\u6a21\u4eff Scala \u8bed\u8a00\u4e2d\u7684\u6982\u5ff5\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u5bb9\u5668\uff0c\u5b83\u53ef\u80fd\u542b\u6709\u67d0\u503c\uff0c\u6216\u8005\u4e0d\u5305\u542b\u3002\u4f7f\u7528\u5b83\u7684\u76ee\u7684\u662f\u5c3d\u53ef\u80fd\u907f\u514d NullPointerException\u3002<\/p>\n<h5>\u6e05\u5355 14. Optional \u7684\u4e24\u4e2a\u7528\u4f8b<\/h5>\n<pre><code>String strA = \" abcd \", strB = null;\nprint(strA);\nprint(\"\");\nprint(strB);\ngetLength(strA);\ngetLength(\"\");\ngetLength(strB);\npublic static void print(String text) {\n \/\/ Java 8\n Optional.ofNullable(text).ifPresent(System.out::println);\n \/\/ Pre-Java 8\n if (text != null) {\n System.out.println(text);\n }\n }\npublic static int getLength(String text) {\n \/\/ Java 8\nreturn Optional.ofNullable(text).map(String::length).orElse(-1);\n \/\/ Pre-Java 8\n\/\/ return if (text != null) ? text.length() : -1;\n };\n<\/code><\/pre>\n<p>\u5728\u66f4\u590d\u6742\u7684 if (xx != null) \u7684\u60c5\u51b5\u4e2d\uff0c\u4f7f\u7528 Optional \u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u66f4\u597d\uff0c\u800c\u4e14\u5b83\u63d0\u4f9b\u7684\u662f\u7f16\u8bd1\u65f6\u68c0\u67e5\uff0c\u80fd\u6781\u5927\u7684\u964d\u4f4e NPE \u8fd9\u79cd Runtime Exception \u5bf9\u7a0b\u5e8f\u7684\u5f71\u54cd\uff0c\u6216\u8005\u8feb\u4f7f\u7a0b\u5e8f\u5458\u66f4\u65e9\u7684\u5728\u7f16\u7801\u9636\u6bb5\u5904\u7406\u7a7a\u503c\u95ee\u9898\uff0c\u800c\u4e0d\u662f\u7559\u5230\u8fd0\u884c\u65f6\u518d\u53d1\u73b0\u548c\u8c03\u8bd5\u3002<\/p>\n<p>Stream \u4e2d\u7684 findAny\u3001max\/min\u3001reduce \u7b49\u65b9\u6cd5\u7b49\u8fd4\u56de Optional \u503c\u3002\u8fd8\u6709\u4f8b\u5982 IntStream.average() \u8fd4\u56de OptionalDouble \u7b49\u7b49\u3002<\/p>\n<p><strong>reduce<\/strong><\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u628a Stream \u5143\u7d20\u7ec4\u5408\u8d77\u6765\u3002\u5b83\u63d0\u4f9b\u4e00\u4e2a\u8d77\u59cb\u503c\uff08\u79cd\u5b50\uff09\uff0c\u7136\u540e\u4f9d\u7167\u8fd0\u7b97\u89c4\u5219\uff08BinaryOperator\uff09\uff0c\u548c\u524d\u9762 Stream \u7684\u7b2c\u4e00\u4e2a\u3001\u7b2c\u4e8c\u4e2a\u3001\u7b2c n \u4e2a\u5143\u7d20\u7ec4\u5408\u3002\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u8bf4\uff0c\u5b57\u7b26\u4e32\u62fc\u63a5\u3001\u6570\u503c\u7684 sum\u3001min\u3001max\u3001average \u90fd\u662f\u7279\u6b8a\u7684 reduce\u3002\u4f8b\u5982 Stream \u7684 sum \u5c31\u76f8\u5f53\u4e8e<\/p>\n<p>Integer sum = integers.reduce(0, (a, b) -> a+b); \u6216<\/p>\n<p>Integer sum = integers.reduce(0, Integer::sum);<\/p>\n<p>\u4e5f\u6709\u6ca1\u6709\u8d77\u59cb\u503c\u7684\u60c5\u51b5\uff0c\u8fd9\u65f6\u4f1a\u628a Stream \u7684\u524d\u9762\u4e24\u4e2a\u5143\u7d20\u7ec4\u5408\u8d77\u6765\uff0c\u8fd4\u56de\u7684\u662f Optional\u3002<\/p>\n<h5>\u6e05\u5355 15. reduce \u7684\u7528\u4f8b<\/h5>\n<pre><code>\/\/ \u5b57\u7b26\u4e32\u8fde\u63a5\uff0cconcat = \"ABCD\"\nString concat = Stream.of(\"A\", \"B\", \"C\", \"D\").reduce(\"\", String::concat); \n\/\/ \u6c42\u6700\u5c0f\u503c\uff0cminValue = -3.0\ndouble minValue = Stream.of(-1.5, 1.0, -3.0, -2.0).reduce(Double.MAX_VALUE, Double::min); \n\/\/ \u6c42\u548c\uff0csumValue = 10, \u6709\u8d77\u59cb\u503c\nint sumValue = Stream.of(1, 2, 3, 4).reduce(0, Integer::sum);\n\/\/ \u6c42\u548c\uff0csumValue = 10, \u65e0\u8d77\u59cb\u503c\nsumValue = Stream.of(1, 2, 3, 4).reduce(Integer::sum).get();\n\/\/ \u8fc7\u6ee4\uff0c\u5b57\u7b26\u4e32\u8fde\u63a5\uff0cconcat = \"ace\"\nconcat = Stream.of(\"a\", \"B\", \"c\", \"D\", \"e\", \"F\").\n filter(x -&gt; x.compareTo(\"Z\") &gt; 0).\n reduce(\"\", String::concat);\n<\/code><\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u4f8b\u5982\u7b2c\u4e00\u4e2a\u793a\u4f8b\u7684 reduce()\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\uff08\u7a7a\u767d\u5b57\u7b26\uff09\u5373\u4e3a\u8d77\u59cb\u503c\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff08String::concat\uff09\u4e3a BinaryOperator\u3002\u8fd9\u7c7b\u6709\u8d77\u59cb\u503c\u7684 reduce() \u90fd\u8fd4\u56de\u5177\u4f53\u7684\u5bf9\u8c61\u3002\u800c\u5bf9\u4e8e\u7b2c\u56db\u4e2a\u793a\u4f8b\u6ca1\u6709\u8d77\u59cb\u503c\u7684 reduce()\uff0c\u7531\u4e8e\u53ef\u80fd\u6ca1\u6709\u8db3\u591f\u7684\u5143\u7d20\uff0c\u8fd4\u56de\u7684\u662f Optional\uff0c\u8bf7\u7559\u610f\u8fd9\u4e2a\u533a\u522b\u3002<\/p>\n<p><strong>limit\/skip<\/strong><\/p>\n<p>limit \u8fd4\u56de Stream \u7684\u524d\u9762 n \u4e2a\u5143\u7d20\uff1bskip \u5219\u662f\u6254\u6389\u524d n \u4e2a\u5143\u7d20\uff08\u5b83\u662f\u7531\u4e00\u4e2a\u53eb subStream \u7684\u65b9\u6cd5\u6539\u540d\u800c\u6765\uff09\u3002<\/p>\n<h5>\u6e05\u5355 16. limit \u548c skip \u5bf9\u8fd0\u884c\u6b21\u6570\u7684\u5f71\u54cd<\/h5>\n<pre><code>public void testLimitAndSkip() {\n List&lt;Person&gt; persons = new ArrayList();\n for (int i = 1; i &lt;= 10000; i++) {\n Person person = new Person(i, \"name\" + i);\n persons.add(person);\n }\nList&lt;String&gt; personList2 = persons.stream().\nmap(Person::getName).limit(10).skip(3).collect(Collectors.toList());\n System.out.println(personList2);\n}\nprivate class Person {\n public int no;\n private String name;\n public Person (int no, String name) {\n this.no = no;\n this.name = name;\n }\n public String getName() {\n System.out.println(name);\n return name;\n }\n}\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code>name1\nname2\nname3\nname4\nname5\nname6\nname7\nname8\nname9\nname10\n[name4, name5, name6, name7, name8, name9, name10]\n<\/code><\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2a\u6709 10\uff0c000 \u4e2a\u5143\u7d20\u7684 Stream\uff0c\u4f46\u5728 short-circuiting \u64cd\u4f5c limit \u548c skip \u7684\u4f5c\u7528\u4e0b\uff0c\u7ba1\u9053\u4e2d map \u64cd\u4f5c\u6307\u5b9a\u7684 getName() \u65b9\u6cd5\u7684\u6267\u884c\u6b21\u6570\u4e3a limit \u6240\u9650\u5b9a\u7684 10 \u6b21\uff0c\u800c\u6700\u7ec8\u8fd4\u56de\u7ed3\u679c\u5728\u8df3\u8fc7\u524d 3 \u4e2a\u5143\u7d20\u540e\u53ea\u6709\u540e\u9762 7 \u4e2a\u8fd4\u56de\u3002<\/p>\n<p>\u6709\u4e00\u79cd\u60c5\u51b5\u662f limit\/skip \u65e0\u6cd5\u8fbe\u5230 short-circuiting \u76ee\u7684\u7684\uff0c\u5c31\u662f\u628a\u5b83\u4eec\u653e\u5728 Stream \u7684\u6392\u5e8f\u64cd\u4f5c\u540e\uff0c\u539f\u56e0\u8ddf sorted \u8fd9\u4e2a intermediate \u64cd\u4f5c\u6709\u5173\uff1a\u6b64\u65f6\u7cfb\u7edf\u5e76\u4e0d\u77e5\u9053 Stream \u6392\u5e8f\u540e\u7684\u6b21\u5e8f\u5982\u4f55\uff0c\u6240\u4ee5 sorted \u4e2d\u7684\u64cd\u4f5c\u770b\u4e0a\u53bb\u5c31\u50cf\u5b8c\u5168\u6ca1\u6709\u88ab limit \u6216\u8005 skip \u4e00\u6837\u3002<\/p>\n<h5>\u6e05\u5355 17. limit \u548c skip \u5bf9 sorted \u540e\u7684\u8fd0\u884c\u6b21\u6570\u65e0\u5f71\u54cd<\/h5>\n<pre><code>List&lt;Person&gt; persons = new ArrayList();\n for (int i = 1; i &lt;= 5; i++) {\n Person person = new Person(i, \"name\" + i);\n persons.add(person);\n }\nList&lt;Person&gt; personList2 = persons.stream().sorted((p1, p2) -&gt; \np1.getName().compareTo(p2.getName())).limit(2).collect(Collectors.toList());\nSystem.out.println(personList2);\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u793a\u4f8b\u5bf9\u6e05\u5355 13 \u505a\u4e86\u5fae\u8c03\uff0c\u9996\u5148\u5bf9 5 \u4e2a\u5143\u7d20\u7684 Stream \u6392\u5e8f\uff0c\u7136\u540e\u8fdb\u884c limit \u64cd\u4f5c\u3002\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code>name2\nname1\nname3\nname2\nname4\nname3\nname5\nname4\n[stream.StreamDW$Person@816f27d, stream.StreamDW$Person@87aac27]\n<\/code><\/pre>\n<p>\u5373\u867d\u7136\u6700\u540e\u7684\u8fd4\u56de\u5143\u7d20\u6570\u91cf\u662f 2\uff0c\u4f46\u6574\u4e2a\u7ba1\u9053\u4e2d\u7684 sorted \u8868\u8fbe\u5f0f\u6267\u884c\u6b21\u6570\u6ca1\u6709\u50cf\u524d\u9762\u4f8b\u5b50\u76f8\u5e94\u51cf\u5c11\u3002<\/p>\n<p>\u6700\u540e\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u4e00\u4e2a parallel \u7684 Steam \u7ba1\u9053\u6765\u8bf4\uff0c\u5982\u679c\u5176\u5143\u7d20\u662f\u6709\u5e8f\u7684\uff0c\u90a3\u4e48 limit \u64cd\u4f5c\u7684\u6210\u672c\u4f1a\u6bd4\u8f83\u5927\uff0c\u56e0\u4e3a\u5b83\u7684\u8fd4\u56de\u5bf9\u8c61\u5fc5\u987b\u662f\u524d n \u4e2a\u4e5f\u6709\u4e00\u6837\u6b21\u5e8f\u7684\u5143\u7d20\u3002\u53d6\u800c\u4ee3\u4e4b\u7684\u7b56\u7565\u662f\u53d6\u6d88\u5143\u7d20\u95f4\u7684\u6b21\u5e8f\uff0c\u6216\u8005\u4e0d\u8981\u7528 parallel Stream\u3002<\/p>\n<p><strong>sorted<\/strong><\/p>\n<p>\u5bf9 Stream \u7684\u6392\u5e8f\u901a\u8fc7 sorted \u8fdb\u884c\uff0c\u5b83\u6bd4\u6570\u7ec4\u7684\u6392\u5e8f\u66f4\u5f3a\u4e4b\u5904\u5728\u4e8e\u4f60\u53ef\u4ee5\u9996\u5148\u5bf9 Stream \u8fdb\u884c\u5404\u7c7b map\u3001filter\u3001limit\u3001skip \u751a\u81f3 distinct \u6765\u51cf\u5c11\u5143\u7d20\u6570\u91cf\u540e\uff0c\u518d\u6392\u5e8f\uff0c\u8fd9\u80fd\u5e2e\u52a9\u7a0b\u5e8f\u660e\u663e\u7f29\u77ed\u6267\u884c\u65f6\u95f4\u3002\u6211\u4eec\u5bf9\u6e05\u5355 14 \u8fdb\u884c\u4f18\u5316\uff1a<\/p>\n<h5>\u6e05\u5355 18. \u4f18\u5316\uff1a\u6392\u5e8f\u524d\u8fdb\u884c limit \u548c skip<\/h5>\n<pre><code>List&lt;Person&gt; persons = new ArrayList();\n for (int i = 1; i &lt;= 5; i++) {\n Person person = new Person(i, \"name\" + i);\n persons.add(person);\n }\nList&lt;Person&gt; personList2 = persons.stream().limit(2).sorted((p1, p2) -&gt; p1.getName().compareTo(p2.getName())).collect(Collectors.toList());\nSystem.out.println(personList2);\n<\/code><\/pre>\n<p>\u7ed3\u679c\u4f1a\u7b80\u5355\u5f88\u591a\uff1a<\/p>\n<pre><code>name2\nname1\n[stream.StreamDW$Person@6ce253f1, stream.StreamDW$Person@53d8d10a]\n<\/code><\/pre>\n<p>\u5f53\u7136\uff0c\u8fd9\u79cd\u4f18\u5316\u662f\u6709 business logic \u4e0a\u7684\u5c40\u9650\u6027\u7684\uff1a\u5373\u4e0d\u8981\u6c42\u6392\u5e8f\u540e\u518d\u53d6\u503c\u3002<\/p>\n<p><strong>min\/max\/distinct<\/strong><\/p>\n<p>min \u548c max \u7684\u529f\u80fd\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bf9 Stream \u5143\u7d20\u5148\u6392\u5e8f\uff0c\u518d findFirst \u6765\u5b9e\u73b0\uff0c\u4f46\u524d\u8005\u7684\u6027\u80fd\u4f1a\u66f4\u597d\uff0c\u4e3a O(n)\uff0c\u800c sorted \u7684\u6210\u672c\u662f O(n log n)\u3002\u540c\u65f6\u5b83\u4eec\u4f5c\u4e3a\u7279\u6b8a\u7684 reduce \u65b9\u6cd5\u88ab\u72ec\u7acb\u51fa\u6765\u4e5f\u662f\u56e0\u4e3a\u6c42\u6700\u5927\u6700\u5c0f\u503c\u662f\u5f88\u5e38\u89c1\u7684\u64cd\u4f5c\u3002<\/p>\n<h5>\u6e05\u5355 19. \u627e\u51fa\u6700\u957f\u4e00\u884c\u7684\u957f\u5ea6<\/h5>\n<pre><code>BufferedReader br = new BufferedReader(new FileReader(\"c:\\\\SUService.log\"));\nint longest = br.lines().\n mapToInt(String::length).\n max().\n getAsInt();\nbr.close();\nSystem.out.println(longest);\n<\/code><\/pre>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u5219\u4f7f\u7528 distinct \u6765\u627e\u51fa\u4e0d\u91cd\u590d\u7684\u5355\u8bcd\u3002<\/p>\n<h5>\u6e05\u5355 20. \u627e\u51fa\u5168\u6587\u7684\u5355\u8bcd\uff0c\u8f6c\u5c0f\u5199\uff0c\u5e76\u6392\u5e8f<\/h5>\n<pre><code>List&lt;String&gt; words = br.lines().\n flatMap(line -&gt; Stream.of(line.split(\" \"))).\n filter(word -&gt; word.length() &gt; 0).\n map(String::toLowerCase).\n distinct().\n sorted().\n collect(Collectors.toList());\nbr.close();\nSystem.out.println(words);\n<\/code><\/pre>\n<p><strong>Match<\/strong><\/p>\n<p>Stream \u6709\u4e09\u4e2a match \u65b9\u6cd5\uff0c\u4ece\u8bed\u4e49\u4e0a\u8bf4\uff1a<\/p>\n<ul>\n<li>allMatch\uff1aStream \u4e2d\u5168\u90e8\u5143\u7d20\u7b26\u5408\u4f20\u5165\u7684 predicate\uff0c\u8fd4\u56de true<\/li>\n<li>anyMatch\uff1aStream \u4e2d\u53ea\u8981\u6709\u4e00\u4e2a\u5143\u7d20\u7b26\u5408\u4f20\u5165\u7684 predicate\uff0c\u8fd4\u56de true<\/li>\n<li>noneMatch\uff1aStream \u4e2d\u6ca1\u6709\u4e00\u4e2a\u5143\u7d20\u7b26\u5408\u4f20\u5165\u7684 predicate\uff0c\u8fd4\u56de true<\/li>\n<\/ul>\n<p>\u5b83\u4eec\u90fd\u4e0d\u662f\u8981\u904d\u5386\u5168\u90e8\u5143\u7d20\u624d\u80fd\u8fd4\u56de\u7ed3\u679c\u3002\u4f8b\u5982 allMatch \u53ea\u8981\u4e00\u4e2a\u5143\u7d20\u4e0d\u6ee1\u8db3\u6761\u4ef6\uff0c\u5c31 skip \u5269\u4e0b\u7684\u6240\u6709\u5143\u7d20\uff0c\u8fd4\u56de false\u3002\u5bf9\u6e05\u5355 13 \u4e2d\u7684 Person \u7c7b\u7a0d\u505a\u4fee\u6539\uff0c\u52a0\u5165\u4e00\u4e2a age \u5c5e\u6027\u548c getAge \u65b9\u6cd5\u3002<\/p>\n<h5>\u6e05\u5355 21. \u4f7f\u7528 Match<\/h5>\n<pre><code>List&lt;Person&gt; persons = new ArrayList();\npersons.add(new Person(1, \"name\" + 1, 10));\npersons.add(new Person(2, \"name\" + 2, 21));\npersons.add(new Person(3, \"name\" + 3, 34));\npersons.add(new Person(4, \"name\" + 4, 6));\npersons.add(new Person(5, \"name\" + 5, 55));\nboolean isAllAdult = persons.stream().\n allMatch(p -&gt; p.getAge() &gt; 18);\nSystem.out.println(\"All are adult? \" + isAllAdult);\nboolean isThereAnyChild = persons.stream().\n anyMatch(p -&gt; p.getAge() &lt; 12);\nSystem.out.println(\"Any child? \" + isThereAnyChild);\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>All are adult? false\n Any child? true\n<\/code><\/pre>\n<p><strong>Stream.generate<\/strong><\/p>\n<p>\u901a\u8fc7\u5b9e\u73b0 Supplier \u63a5\u53e3\uff0c\u4f60\u53ef\u4ee5\u81ea\u5df1\u6765\u63a7\u5236\u6d41\u7684\u751f\u6210\u3002\u8fd9\u79cd\u60c5\u5f62\u901a\u5e38\u7528\u4e8e\u968f\u673a\u6570\u3001\u5e38\u91cf\u7684 Stream\uff0c\u6216\u8005\u9700\u8981\u524d\u540e\u5143\u7d20\u95f4\u7ef4\u6301\u7740\u67d0\u79cd\u72b6\u6001\u4fe1\u606f\u7684 Stream\u3002\u628a Supplier \u5b9e\u4f8b\u4f20\u9012\u7ed9 Stream.generate() \u751f\u6210\u7684 Stream\uff0c\u9ed8\u8ba4\u662f\u4e32\u884c\uff08\u76f8\u5bf9 parallel \u800c\u8a00\uff09\u4f46\u65e0\u5e8f\u7684\uff08\u76f8\u5bf9 ordered \u800c\u8a00\uff09\u3002\u7531\u4e8e\u5b83\u662f\u65e0\u9650\u7684\uff0c\u5728\u7ba1\u9053\u4e2d\uff0c\u5fc5\u987b\u5229\u7528 limit \u4e4b\u7c7b\u7684\u64cd\u4f5c\u9650\u5236 Stream \u5927\u5c0f\u3002<\/p>\n<h5>\u6e05\u5355 22. \u751f\u6210 10 \u4e2a\u968f\u673a\u6574\u6570<\/h5>\n<pre><code>Random seed = new Random();\nSupplier&lt;Integer&gt; random = seed::nextInt;\nStream.generate(random).limit(10).forEach(System.out::println);\n\/\/Another way\nIntStream.generate(() -&gt; (int) (System.nanoTime() % 100)).\nlimit(10).forEach(System.out::println);\n<\/code><\/pre>\n<p>Stream.generate() \u8fd8\u63a5\u53d7\u81ea\u5df1\u5b9e\u73b0\u7684 Supplier\u3002\u4f8b\u5982\u5728\u6784\u9020\u6d77\u91cf\u6d4b\u8bd5\u6570\u636e\u7684\u65f6\u5019\uff0c\u7528\u67d0\u79cd\u81ea\u52a8\u7684\u89c4\u5219\u7ed9\u6bcf\u4e00\u4e2a\u53d8\u91cf\u8d4b\u503c\uff1b\u6216\u8005\u4f9d\u636e\u516c\u5f0f\u8ba1\u7b97 Stream \u7684\u6bcf\u4e2a\u5143\u7d20\u503c\u3002\u8fd9\u4e9b\u90fd\u662f\u7ef4\u6301\u72b6\u6001\u4fe1\u606f\u7684\u60c5\u5f62\u3002<\/p>\n<h5>\u6e05\u5355 23. \u81ea\u5b9e\u73b0 Supplier<\/h5>\n<pre><code>Stream.generate(new PersonSupplier()).\nlimit(10).\nforEach(p -&gt; System.out.println(p.getName() + \", \" + p.getAge()));\nprivate class PersonSupplier implements Supplier&lt;Person&gt; {\n private int index = 0;\n private Random random = new Random();\n @Override\n public Person get() {\n return new Person(index++, \"StormTestUser\" + index, random.nextInt(100));\n }\n}\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>StormTestUser1, 9\nStormTestUser2, 12\nStormTestUser3, 88\nStormTestUser4, 51\nStormTestUser5, 22\nStormTestUser6, 28\nStormTestUser7, 81\nStormTestUser8, 51\nStormTestUser9, 4\nStormTestUser10, 76\n<\/code><\/pre>\n<p><strong>Stream.iterate<\/strong><\/p>\n<p>iterate \u8ddf reduce \u64cd\u4f5c\u5f88\u50cf\uff0c\u63a5\u53d7\u4e00\u4e2a\u79cd\u5b50\u503c\uff0c\u548c\u4e00\u4e2a UnaryOperator\uff08\u4f8b\u5982 f\uff09\u3002\u7136\u540e\u79cd\u5b50\u503c\u6210\u4e3a Stream \u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0cf(seed) \u4e3a\u7b2c\u4e8c\u4e2a\uff0cf(f(seed)) \u7b2c\u4e09\u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<p><strong>\u6e05\u5355 24. \u751f\u6210\u4e00\u4e2a\u7b49\u5dee\u6570\u5217<\/strong><\/p>\n<pre><code>Stream.iterate(0, n -&gt; n + 3).limit(10). forEach(x -&gt; System.out.print(x + \" \"));.\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>0 3 6 9 12 15 18 21 24 27\n<\/code><\/pre>\n<p>\u4e0e Stream.generate \u76f8\u4eff\uff0c\u5728 iterate \u65f6\u5019\u7ba1\u9053\u5fc5\u987b\u6709 limit \u8fd9\u6837\u7684\u64cd\u4f5c\u6765\u9650\u5236 Stream \u5927\u5c0f\u3002<\/p>\n<h3>\u8fdb\u9636\uff1a\u7528 Collectors \u6765\u8fdb\u884c reduction \u64cd\u4f5c<\/h3>\n<p>java.util.stream.Collectors \u7c7b\u7684\u4e3b\u8981\u4f5c\u7528\u5c31\u662f\u8f85\u52a9\u8fdb\u884c\u5404\u7c7b\u6709\u7528\u7684 reduction \u64cd\u4f5c\uff0c\u4f8b\u5982\u8f6c\u53d8\u8f93\u51fa\u4e3a Collection\uff0c\u628a Stream \u5143\u7d20\u8fdb\u884c\u5f52\u7ec4\u3002<\/p>\n<p><strong>groupingBy\/partitioningBy<\/strong><\/p>\n<h5>\u6e05\u5355 25. \u6309\u7167\u5e74\u9f84\u5f52\u7ec4<\/h5>\n<pre><code>Map&lt;Integer, List&lt;Person&gt;&gt; personGroups = Stream.generate(new PersonSupplier()).\n limit(100).\n collect(Collectors.groupingBy(Person::getAge));\nIterator it = personGroups.entrySet().iterator();\nwhile (it.hasNext()) {\n Map.Entry&lt;Integer, List&lt;Person&gt;&gt; persons = (Map.Entry) it.next();\n System.out.println(\"Age \" + persons.getKey() + \" = \" + persons.getValue().size());\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684 code\uff0c\u9996\u5148\u751f\u6210 100 \u4eba\u7684\u4fe1\u606f\uff0c\u7136\u540e\u6309\u7167\u5e74\u9f84\u5f52\u7ec4\uff0c\u76f8\u540c\u5e74\u9f84\u7684\u4eba\u653e\u5230\u540c\u4e00\u4e2a list \u4e2d\uff0c\u53ef\u4ee5\u770b\u5230\u5982\u4e0b\u7684\u8f93\u51fa\uff1a<\/p>\n<pre><code>Age 0 = 2\nAge 1 = 2\nAge 5 = 2\nAge 8 = 1\nAge 9 = 1\nAge 11 = 2\n\u2026\u2026\n<\/code><\/pre>\n<pre><code>Map&lt;Boolean, List&lt;Person&gt;&gt; children = Stream.generate(new PersonSupplier()).\n limit(100).\n collect(Collectors.partitioningBy(p -&gt; p.getAge() &lt; 18));\nSystem.out.println(\"Children number: \" + children.get(true).size());\nSystem.out.println(\"Adult number: \" + children.get(false).size());\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>Children number: 23 \n     Adult number: 77\n<\/code><\/pre>\n<p>\u5728\u4f7f\u7528\u6761\u4ef6\u201c\u5e74\u9f84\u5c0f\u4e8e 18\u201d\u8fdb\u884c\u5206\u7ec4\u540e\u53ef\u4ee5\u770b\u5230\uff0c\u4e0d\u5230 18 \u5c81\u7684\u672a\u6210\u5e74\u4eba\u662f\u4e00\u7ec4\uff0c\u6210\u5e74\u4eba\u662f\u53e6\u5916\u4e00\u7ec4\u3002partitioningBy \u5176\u5b9e\u662f\u4e00\u79cd\u7279\u6b8a\u7684 groupingBy\uff0c\u5b83\u4f9d\u7167\u6761\u4ef6\u6d4b\u8bd5\u7684\u662f\u5426\u4e24\u79cd\u7ed3\u679c\u6765\u6784\u9020\u8fd4\u56de\u7684\u6570\u636e\u7ed3\u6784\uff0cget(true) \u548c get(false) \u80fd\u5373\u4e3a\u5168\u90e8\u7684\u5143\u7d20\u5bf9\u8c61\u3002<\/p>\n<h2>\u7ed3\u675f\u8bed<\/h2>\n<p>\u603b\u4e4b\uff0cStream \u7684\u7279\u6027\u53ef\u4ee5\u5f52\u7eb3\u4e3a\uff1a<\/p>\n<ul>\n<li>\u5b83\u6ca1\u6709\u5185\u90e8\u5b58\u50a8\uff0c\u5b83\u53ea\u662f\u7528\u64cd\u4f5c\u7ba1\u9053\u4ece source\uff08\u6570\u636e\u7ed3\u6784\u3001\u6570\u7ec4\u3001generator function\u3001IO channel\uff09\u6293\u53d6\u6570\u636e\u3002<\/li>\n<li>\n<p>\u5b83\u4e5f\u7edd\u4e0d\u4fee\u6539\u81ea\u5df1\u6240\u5c01\u88c5\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u7684\u6570\u636e\u3002\u4f8b\u5982 Stream \u7684 filter \u64cd\u4f5c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u4e0d\u5305\u542b\u88ab\u8fc7\u6ee4\u5143\u7d20\u7684\u65b0 Stream\uff0c\u800c\u4e0d\u662f\u4ece source \u5220\u9664\u90a3\u4e9b\u5143\u7d20\u3002<\/p>\n<\/li>\n<li>\n<p>\u6240\u6709 Stream \u7684\u64cd\u4f5c\u5fc5\u987b\u4ee5 lambda \u8868\u8fbe\u5f0f\u4e3a\u53c2\u6570<\/p>\n<\/li>\n<li>\n<p>\u4e0d\u652f\u6301\u7d22\u5f15\u8bbf\u95ee<\/p>\n<\/li>\n<li>\n<p>\u4f60\u53ef\u4ee5\u8bf7\u6c42\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4f46\u65e0\u6cd5\u8bf7\u6c42\u7b2c\u4e8c\u4e2a\uff0c\u7b2c\u4e09\u4e2a\uff0c\u6216\u6700\u540e\u4e00\u4e2a\u3002\u4e0d\u8fc7\u8bf7\u53c2\u9605\u4e0b\u4e00\u9879\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f88\u591a Stream \u64cd\u4f5c\u662f\u5411\u540e\u5ef6\u8fdf\u7684\uff0c\u4e00\u76f4\u5230\u5b83\u5f04\u6e05\u695a\u4e86\u6700\u540e\u9700\u8981\u591a\u5c11\u6570\u636e\u624d\u4f1a\u5f00\u59cb\u3002<\/p>\n<\/li>\n<li>\n<p>Intermediate \u64cd\u4f5c\u6c38\u8fdc\u662f\u60f0\u6027\u5316\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u4e00\u4e2a Stream \u662f\u5e76\u884c\u5316\u7684\uff0c\u5c31\u4e0d\u9700\u8981\u518d\u5199\u591a\u7ebf\u7a0b\u4ee3\u7801\uff0c\u6240\u6709\u5bf9\u5b83\u7684\u64cd\u4f5c\u4f1a\u81ea\u52a8\u5e76\u884c\u8fdb\u884c\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ef\u4ee5\u662f\u65e0\u9650\u7684<\/p>\n<\/li>\n<li>\n<p>\u96c6\u5408\u6709\u56fa\u5b9a\u5927\u5c0f\uff0cStream \u5219\u4e0d\u5fc5\u3002limit(n) \u548c findFirst() \u8fd9\u7c7b\u7684 short-circuiting \u64cd\u4f5c\u53ef\u4ee5\u5bf9\u65e0\u9650\u7684 Stream \u8fdb\u884c\u8fd0\u7b97\u5e76\u5f88\u5feb\u5b8c\u6210\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Java 8 \u4e2d\u7684 Streams API \u8be6\u89e3 Streams \u7684\u80cc\u666f\uff0c\u4ee5\u53ca Java 8 \u4e2d\u7684\u4f7f\u7528\u8be6\u89e3 \u4e3a\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2020\/01\/15\/java-8-%e4%b8%ad%e7%9a%84-streams-api-%e8%af%a6%e8%a7%a3\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-1097","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=1097"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1097\/revisions"}],"predecessor-version":[{"id":1365,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1097\/revisions\/1365"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=1097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=1097"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=1097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}